0

我有一个包含以下字段的 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. 查询返回错误值。请帮我。

4

1 回答 1

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
于 2013-10-02T11:22:41.050 回答