2011-12-03 1 views
0
id partner_id date_created status item_id 
3 2 1322861013 redirected 1 
4 2 1322943245 redirected 6 
5 3 1322943246 redirected 6 
6 2 1322943247 redirected 6 

同じitem_idの行数をどのように数えますか?テーブル名は、私は、出力にそれを望む以上のためのordersSQLの行数を合計する

例である:私はこれだけもう少し複雑ないくつかのPHPで行う方法を知って

There's 3 rows with the item_id 6 
There's 1 rows with the item_id 1 

答えて

0
SELECT item_id, COUNT(id) FROM orders 
GROUP BY item_id 
1
mysql> create table test(
    ->  row integer 
    ->) ; 
Query OK, 0 rows affected (0.05 sec) 

mysql> insert into test values (3) , (3) , (3) , (1) ; 
Query OK, 4 rows affected (0.00 sec) 
Records: 4 Duplicates: 0 Warnings: 0 

mysql> select count(*) from test group by row; 
+----------+ 
| count(*) | 
+----------+ 
|  1 | 
|  3 | 
+----------+ 
2 rows in set (0.03 sec) 

mysql> 
+0

+1良い例 –

2

ループここで使用するパターンは、GROUP BYと組み合わせたCOUNT()です。

SELECT item_id, COUNT(id) AS item_count FROM orders GROUP BY item_id 

必要に応じて簡単に再フォーマットできる2つの列が表示されます。