mysql计数多行在哪里

时间:2011-04-27 16:13:16

标签: php mysql

我的问题类似于这个问题MySQL COUNT() multiple columns

但对我来说,我如何计算多行:

report table:

id     t_pro      t_price
1.    voucher    20.00
2.    pro        13.00
3.    pro 1x     13.00
4.    pro 2x       13.00
5.    voucher    20.00
6.    pro 1x       13.00 

$db->query("SELECT COUNT(*) FROM (SELECT t_pro WHERE t_pro LIKE 'pro%' AS pro_tan, SELECT t_pro WHERE t_pro LIKE 'voucher%' AS voucher) WHERE date=CURDATE()  ");

结果如下:

voucher : 2
pro tan : 4

3 个答案:

答案 0 :(得分:3)

SELECT  CASE WHEN t_pro LIKE 'voucher%' THEN 'voucher' WHEN t_pro LIKE 'pro%' THEN 'pro tan' END AS pro,
        COUNT(*)
FROM    mytable
WHERE   t_pro LIKE 't_pro%' OR t_pro LIKE 'voucher%'
GROUP BY
        pro

答案 1 :(得分:2)

您可以使用GROUP BY

SELECT t_pro, COUNT(*) FROM report GROUP BY t_pro;

注意:这是您想要每个t_pro值的计数。否则,您需要修改自定义GROUP BY值关联的t_pro

答案 2 :(得分:0)

select t_pro, count(1) from table group by t_pro;