2012-11-20 135 views
0

我在遇到以下MySQL情況的一些問題。MySQL GROUP BY多列獨佔

我有以下表結構:

+----+---------+-----------------+------------------+--------------------------------------+------------+ 
| id | author | recipient |  type  |     data    |  date | 
+----+---------+-----------------+------------------+--------------------------------------+------------+ 
| 2 | 1 |  0  | added_item  | { "item_id": "18" }     | 1352982623 | 
| 11 | 1 |  7  | started_follow |          | 1353030567 | 
| 9 | 1 |  0  | added_collection | { "collection_id": "18" }   | 1352994561 | 
| 12 | 7 |  1  | started_follow |          | 1353030952 | 
| 8 | 1 |  1  | started_follow |          | 1352987839 | 
| 14 | 1 |  0  | added_collection | { "collection_id": "19" }   | 1353056624 | 
| 15 | 7 |  0  | added_collection | { "collection_id": "20" }   | 1353056731 | 
| 16 | 1 |  7  | started_follow |          | 1353057009 | 
| 17 | 7 |  0  | added_item  | { "item_id": "21" }     | 1353057054 | 
| 24 | 2 |  0  | added_image  | { "image_id": "3", "item_id": "23" } | 1353419642 | 
| 23 | 2 |  0  | added_item  | { "item_id": "23" }     | 1353419159 | 
| 22 | 2 |  0  | added_collection | { "collection_id": "22" }   | 1353419146 | 
| 25 | 2 |  0  | added_image  | { "image_id": "4", "item_id": "23" } | 1353419644 | 
| 26 | 2 |  0  | added_image  | { "image_id": "5", "item_id": "23" } | 1353419646 | 
| 27 | 2 |  0  | added_collection | { "collection_id": "23" }   | 1353419953 | 
| 28 | 2 |  0  | added_item  | { "item_id": "24" }     | 1353419961 | 
| 29 | 2 |  0  | added_image  | { "image_id": "6", "item_id": "24" } | 1353419982 | 
| 30 | 2 |  0  | added_item  | { "item_id": "25" }     | 1353420424 | 
| 31 | 2 |  0  | added_image  | { "image_id": "7", "item_id": "25" } | 1353420435 | 
+----+---------+-----------------+------------------+--------------------------------------+------------+ 

我希望做的是組秒數,其中authortype字段相同內的所有故事。

下面是我用此刻的查詢,但這次不返回故事的所有實例,只在第一次每個typeusers發生:

SELECT `activity`.`id` AS `id` 
FROM `activity` 
INNER JOIN `user_privacy` ON `user_privacy`.`user_id` = `activity`.`author` 
WHERE (
`activity`.`author` =2 
OR `activity`.`recipient` =2 
) 
AND `user_privacy`.`include_stream` =1 
GROUP BY ROUND(UNIX_TIMESTAMP(DATE) /300) , `type` 
ORDER BY `activity`.`date` DESC 
LIMIT 300 

我已成立了一個SQLFiddle演示這裏>http://sqlfiddle.com/#!2/2b5f4/1

鑑於數據文所概述,和所使用的查詢,我想返回以下id S:

回答

2

也許你誤認爲UNIX_TIMESTAMP和FROM_UNIXTIME。只需重新設定它,並且您想要的結果集就在那裏。 fiddle here

+0

謝謝,就是這樣! – BenM