简体   繁体   中英

Order Group By in Activity Stream by Date MYSQL

I tried a lot of solutions from here, but none of those solved my issue! I have the following table structure:

|Column|Type|
|------
|activity_id| int(10) PRIMARY
|user_id| int(10)
|type| varchar(255)
|activity| varchar(255)
|item_id| int(11)
|secundary_id| int(11)
|date| datetime

I'm building an Activity Stream, in this table i'll record like

"John (user_id) commented (activity) a post (type => item_id) 5 hours ago (date)"

I intend to do the following structure:

"John,Marie, and +2 commented a post 5 hours ago"

So far i was able to do this connection, but i can't successfully order the records by date. I tried some inner joins in the own table, but with that i was unable to retrieve the other activity types that wasn't grouped by (type,activity,item_id), for example:

"John ( user_id) posted (activity) a post(type => item_id ) 5 hours ago (date)"

When i use JOINS i only get the grouped by records. This is my actual query:

SELECT t.*, GROUP_CONCAT(DISTINCT user_id ) AS users FROM (
SELECT *
FROM activity
   ORDER BY date DESC 
)t
GROUP BY type, activity, item_id
ORDER BY date DESC

I already tried some solutions listed here: Select the 3 most recent records where the values of one column are distinct But when i join the tables, i lost the non grouped records! Does anyone have another thoughts? Thanks by now!

/** EDIT - RESULTS

activity_id     user_id     type    activity    item_id     secundary_id    date    users
865         32  update  comment     20  22  2013-10-29 15:03:49     32,34
858         36  update  post    20  NULL    2013-10-29 13:50:59     36
864         32  post    comment     2615    21  2013-10-29 14:55:58     32,34,36
856         36  post    comment     2616    14  2013-10-29 13:50:10     36,39,34,32
872         32  post    comment     2617    28  2013-10-29 15:46:20     32
852         34  post    post    2615    NULL    2013-10-29 13:47:25     34
854         32  post    post    2616    NULL    2013-10-29 13:49:13     32
870         32  post    post    2617    NULL    2013-10-29 15:36:57     32

That's what i get with the query above, but for example the item_id 2616 that have 4 user comments, are not ordering by the last user comment date, for example i have the following record:

activity_id user_id type activity item_id secundary_item date
856     36  post    comentar    2616    14  2013-10-29 14:05:10

His date is 14:05, but on my select i get another older comment date!

-- FIDDLE

http://sqlfiddle.com/#!2/1923d2/1


SOLVED by @Tom

If you want to get the most recent date, try this:

ORIGINAL

SELECT   activity_id, user_id, type, activity, item_id, secundary_id, MAX(date) date, 
         GROUP_CONCAT(DISTINCT user_id ) AS users 
FROM     activity
GROUP BY type, activity, item_id
ORDER BY date DESC

EDITED - Below will give you the most recent record for each grouping.

SELECT   activity_id, user_id, type, activity, item_id, secundary_id, date, 
         users

FROM     activity a

         JOIN ( SELECT   MAX(activity_id) max_activity_id,
                         GROUP_CONCAT(DISTINCT user_id ) AS users
                FROM     activity
                GROUP BY type, activity, item_id
              ) AS grp ON a.activity_id = grp.max_activity_id

GROUP BY date DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM