mysql if

Database/Mysql 2016. 1. 30. 20:40

하나의 결제 테이블에 결제 수단과 결제한 금액이 들어 있다.

결제 수단별 결제 횟수와 결제 금액을 출력하는 sql.

컬럼별 subquery 를 돌리지 않고 if 문을 사용하여 조금이나마 서버를 도와주기.


* Subquery


1
2
3
4
5
select 
    (select count(*from order where ds_pay_method = 'VBank' and no_pay_amt > 0) as vbank_cnt,
    (select sum(no_pay_amt) from order where ds_pay_method = 'VBank' and no_pay_amt > 0) as vbank_sum,
    (select count(*from order where ds_pay_method = 'Card' and no_pay_amt > 0) as card_cnt,
    (select sum(no_pay_amt) from order where ds_pay_method = 'VBank' and no_pay_amt > 0) as card_sum
cs



* if condition


1
2
3
4
5
6
7
select
    sum( if(ds_pay_method = 'VBank',1,0) ) as vbank_cnt,
    sum( if(ds_pay_method = 'VBank',no_pay_amt,0 ) ) as vbank_sum,
    sum( if(ds_pay_method = 'Card',1,0) ) as card_cnt,
    sum( if(ds_pay_method = 'Card',no_pay_amt,0 ) ) as card_sum
from order
where no_pay_amt > 0;
cs




WRITTEN BY
손가락귀신
정신 못차리면, 벌 받는다.

,