如何在mysql的单个查询中检查两个条件?

时间:2015-11-03 06:25:10

标签: php mysql

示例

------------------------------------------
Amount     |   Type
------------------------------------------
1000       | credit
------------------------------------------
4500       | credit
------------------------------------------
1250       | debit
------------------------------------------
500        |  credit
------------------------------------------


select IFNULL(SUM(amount),0) as Credit_Amount,IFNULL(SUM(amount),0) as Debit_Amount, 
type,party from receipt where  party='$list[id]' and type='credit' or 
type='debit'
  

输出

Credit_Amount = 6000
Debit_Amount = 1250

如果类型为credit,则值应添加credit变量,如果type为debit,则值应添加debit。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

这是信用查询,只能根据要求追加。

Select IF(type='Credit', SUM(amount), null) as Credit from receipt