[mysql] 'SELECT'문에서 'IF'- 열 값을 기준으로 출력 값 선택


3 Answers

case 문 사용 :

select id,
    case report.type
        when 'P' then amount
        when 'N' then -amount
    end as amount
from
    `report`
Question
SELECT id, amount FROM report

report.type='P' 이면 amount , report.type='N' 이면 -amount 필요합니다. 위의 쿼리에 어떻게 추가합니까?




SELECT id, amount
FROM report
WHERE type='P'

UNION

SELECT id, (amount * -1) AS amount
FROM report
WHERE type = 'N'

ORDER BY id;



select 
  id,
  case 
    when report_type = 'P' 
    then amount 
    when report_type = 'N' 
    then -amount 
    else null 
  end
from table



이 것을 시도해보십시오.

 SELECT
    id , IF(report.type = 'p', IFNULL(amount,0), IFNULL(amount,0) * -1) as amount
 FROM report



Related