select * from z_tmp where if ( char_length(name) = 2, 'Y','N') = 'Y'

  => z_tmp table의 name칼럼에서 두글자만 가져온다.

 

select if( substr(name,1,1) = 'A', 'apple', name) from z_tmp

 => z_tmp table의 name칼럼에서 첫자가 A이면 apple로 치환출력하고, 아니라면 그냥 name명을 출력한다.

 

SELECT * FROM z_tmp ORDER BY IF(bb=3, 0, bb) ASC

 => z_tmp table의 bb칼럼에서 3이면 0으로 치환출력하고 아니면 bb를 출력하는데, 정렬을 asc로 정렬한다.

 

카운트 조회할때

     a.receive_shop_id, c.shop_name, sum(1) as shop_cnt,
     count( if(a.connect_log_type='A', 1, null) ) AS connect_cnt1,
     count( if(a.connect_log_type='P', 1, null) ) AS connect_cnt2 ,
     count( if(a.connect_log_type='', 1, null) ) AS connect_cnt3,

  이렇게도 사용 가능하다.