如何在单个查询中使用mysql案例或子查询

时间:2016-07-18 12:03:54

标签: mysql

我尝试过这样的代码:

SELECT
  ca.batch_code, s.stu_surname, s.cdc_no, s.passport_no,
  p.special_approval, p.special_approval_amount  
FROM  course_apply ca 
  INNER JOIN student s ON ca.stu_code = s.stu_code
  INNER JOIN payment_detail p ON p.stu_code = ca.stu_code
ORDER BY ca.batch_code ASC

我需要这样的输出:

可获得特殊批准意味着显示特殊批准金额,否则为字段值0

1 个答案:

答案 0 :(得分:0)

SELECT
  ca.batch_code, s.stu_surname, s.cdc_no, s.passport_no,
  p.special_approval, 
  case
    when p.special_approval  = 'available' then p.special_approval_amount 
    else 0
    end as approval_amount
FROM  course_apply ca 
  INNER JOIN student s ON ca.stu_code = s.stu_code
  INNER JOIN payment_detail p ON p.stu_code = ca.stu_code
ORDER BY ca.batch_code ASC