BenM
BenM

Reputation: 53246

MySQL GROUP BY Multiple Columns Exclusive

I am having some problems with the following MySQL situation.

I have the following table structure:

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

What I would like to do is group all stories within a number of seconds, where the author and type fields are the same.

Here's the query I'm using at the moment, but this does not return all instances of the stories, only the first time each type occurs per users:

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

I have set up an SQLFiddle demo here > http://sqlfiddle.com/#!2/2b5f4/1.

Given the data outlined above, and the query used, I would like to return the following ids:

Upvotes: 0

Views: 197

Answers (1)

Saic Siquot
Saic Siquot

Reputation: 6513

perhaps did you mistook UNIX_TIMESTAMP and FROM_UNIXTIME. Just repalce it and your desired result set is there. fiddle here

Upvotes: 2

Related Questions