select a.*, count(b.entry) from first_table as a inner join second_table as b on a.idx=b.no group by a.idx;
(left join 형 a table idx = b table no와 동기화, a.idx로 그룹하여 b테이블의 부가정보 획득)
union selection
(select * from first_tabe where idx=1) union (select field1, field2, field3 from second_table where no=1) order by idx desc;
필드명은 처음 셀렉트된것을 따른다. 따라서 alias를 주던가 해야되며 뒤에 테이블이 필드갯수가 틀릴때는 위처럼 맞춰주면된다.
select insert
insert into first_table select * from second_table where idx=2;
필드갯수 일치화 필요
view selection
create view testdb.view_table as select * from second_table;
select * from view_table;
이놈은 tmp table 만드는 삽질없이 바로 뷰트리 태워주는놈이다. mysql 5.0에서만 가능
unique selection
select * from first_table group by aa having count(*) > 1
중복레코드 추적 (aa필드기준)
distinct query
select distinct aa from table group by bb where expr
가급적 쓰지않는게 낫다 group by 보다 퍼포먼스 떨어진다. row exam 갯수가 적을때만 대충 편하게 이용.
제작시 자주쓰이는 쿼리들 나열 -> 이거 이외이 삽질은 안하는것이 좋다
주로 sql에서 쓰이는 function 은
select (is_null, if, substr, concat, date_add, date_sub) 정도
datetime filed의 경우 date function 을 쓰는것이 로드에 효율적이다.
그리고 생각해보니 update if select 서브쿼리들 몇개는 썼었는데 트랜잭션으로 바꿧다 -_-
Posted by LeCieL


