2011-10-25 19 views
1

我想指望有相同的order_id表中的多少行refnumbers數相同的order_id和輸出有多少SQL

所以應該輸出:

There's 5 rows with the order_id 123 

There's 9 rows with the order_id 124 

There's 18 rows with the order_id 125 

There's 2 rows with the order_id 77 

它的列order_is,它應該在表中後refnumbers

我真的不知道如何做到這一點,沒有具體提及一個order_id,然後通過他們在PHP中做一個循環。

回答

4

您需要在要獲取計數的列上使用group by

這不起作用

SELECT count(*) as rowcount 
FROM refnumbers 

會給你一個單行所有rowcounts每個不同的order_id

計數

SELECT order_id, count(*) as rowcount 
FROM refnumbers 
GROUP BY order_id /*WITH ROLLUP*/ 
ORDER BY order_id 

會給你每個不同的計ORDER_ID。
如果您想獲得總數並取消註釋with rollup部分。

+0

非常好的答案! – Karem

相關問題