在mysql中使用count + where时,如何返回所有值?
MySQL的:
SELECT COUNT(*) as count , promocode FROM `dat_booking` WHERE promocode IN ("ABC","DEF","XYZ") GROUP BY promocode;
结果:
count promocode
3 ABC
6 XYZ
但是我也想要如果它在表dat_booking中没有值它返回0
我想:
count promocode
3 ABC
0 DEF
6 XYZ
感谢您阅读
答案 0 :(得分:1)
SELECT 1 as req, promocode,count(*) FROM `dat_booking` WHERE promocode IN ("ABC","DEF","XYZ") GROUP BY promocode union select 2 as req, 'XYZ', 0 order by req
答案 1 :(得分:0)
假设您有另一个表格,用于 promocodes 的静态值:
SELECT
COUNT(*) as count ,
`dat_booking`.promocode
FROM `dat_booking` LEFT JOIN `some_static_promocode_table`.promocode
ON `some_static_promocode_table`.promocode = `dat_booking`.promocode
WHERE `dat_booking`.promocode IN ("ABC","DEF","XYZ") GROUP BY
`dat_booking`.promocode;