SELECT COUNT(*) AS customer, t.state
FROM tbl_parcel_item t
WHERE t.courier_name='Tnt'
AND t.date = '2018-03-12'
AND t.ship_status NOT IN (0,1,9,10)
GROUP BY t.state`
基于上面的sql,结果将是: enter image description here 如何使它得到上面的结果的总和,例如: enter image description here
答案 0 :(得分:0)
使用with rollup
:
SELECT COUNT(*) AS customer, t.state`
FROM tbl_parcel_item t
WHERE t.courier_name = 'Tnt' AND
t.date = '2018-03-12' AND
t.ship_status NOT IN (0, 1, 9, 10)
GROUP BY t.state WITH ROLLUP;
答案 1 :(得分:0)
全部使用联合:
`SELECT COUNT(*) AS customer, t.state`
`FROM tbl_parcel_item t`
`WHERE t.courier_name='Tnt'`
`AND t.date = '2018-03-12'`
`AND t.ship_status NOT IN (0,1,9,10)`
`GROUP BY t.state`
`UNION ALL`
`SELECT COUNT(*) AS customer, 'Total' as state`
`FROM tbl_parcel_item t`
`WHERE t.courier_name='Tnt'`
`AND t.date = '2018-03-12'`
`AND t.ship_status NOT IN (0,1,9,10)`
答案 2 :(得分:0)
在查询上方添加另一个查询,如下所示:
Select sum(customer) as Total
FROM
(
SELECT COUNT(*) AS customer, t.state`
FROM tbl_parcel_item t
WHERE t.courier_name = 'Tnt' AND
t.date = '2018-03-12' AND
t.ship_status NOT IN (0, 1, 9, 10)
GROUP BY t.state
)a;
答案 3 :(得分:0)
使用ROLLUP
:
SELECT
COALESCE(t.state, 'TOTAL') AS state,
COUNT(*) AS customer
FROM tbl_parcel_item t
WHERE
t.courier_name = 'Tnt' AND
t.date = '2018-03-12' AND
t.ship_status NOT IN (0,1,9,10)
GROUP BY t.state WITH ROLLUP;
答案 4 :(得分:0)
01)WITH ROLLUP如其他答案所述
02) 使用联合查询
SELECT t.state, COUNT(*) AS customer FROM tbl_parcel_item t WHERE t.courier_name='Tnt' AND t.date = '2018-03-12' AND t.ship_status NOT IN (0,1,9,10) GROUP BY t.state
UNION
SELECT 'Total', COUNT(*) AS customer FROM tbl_parcel_item t WHERE t.courier_name='Tnt' AND t.date = '2018-03-12' AND t.ship_status NOT IN (0,1,9,10)