从Mysql表中获取计数报告

时间:2013-10-02 11:11:47

标签: mysql sql count

我有一个包含以下字段的Mysql表,

bill_date,bill_no,item,tax,total. 我想生成一个包含字段

的报告 到日期之间

bill_date,Bill_no,Taxable Item(Count),Nontaxable Item(Count)。 我尝试这样的查询

select Bill_no,bill_date,count(tax=0),count(tax>0) from bill group by bill_no。查询返回错误的值。请帮帮我。

1 个答案:

答案 0 :(得分:0)

SELECT bill_no, bill_date, 
    SUM(IF(tax=0, 1, 0)) AS nonTaxableItems,
    SUM(IF(tax>0, 1, 0)) AS taxableItems,
    FROM bill 
    WHERE bill_date BETWEEN '2013-01-01' AND '2013-12-31'
    GROUP BY bill_no

如果您在名为count的列中存储了项目数,则查询将变为:

SELECT bill_no, bill_date, 
    SUM(IF(tax=0, count, 0)) AS nonTaxableItems,
    SUM(IF(tax>0, count, 0)) AS taxableItems,
    FROM bill 
    WHERE bill_date BETWEEN '2013-01-01' AND '2013-12-31'
    GROUP BY bill_no