2011-04-27 117 views
1

我的問題類似於這樣的一個MySQL COUNT() multiple columnsmysql count multiple rows

但對我來說,我怎麼算複式行:

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 
+0

? – Neal 2011-04-27 16:15:57

回答

2

您可以使用GROUP BY

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

注意:這是如果你想要每個t_pro值的計數。否則,你需要修改GROUP BY定製t_pro值協會爲什麼您使用中的一個SELECT

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

@Quassnoi:謝謝。我如何迴應結果? 'echo pro tan = $ r ['pro']'?? – tonoslfx 2011-04-27 16:21:49

+0

@ boyee007:你在用什麼框架? – Quassnoi 2011-04-27 16:26:44

+0

即時通訊不使用任何框架。其自定義代碼。我如何迴應結果,如'憑證:2,親譚:4' – tonoslfx 2011-04-27 16:28:39

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