I have a feed application that I am trying to group results from consecutively.
My table looks like this:
postid | posttype | target | action | date | title | content
1 | userid | NULL | upgrade | 0000-01-00 00:00:00 | Upgraded 1 | exmple
1 | userid | NULL | upgrade | 0000-01-00 00:00:01 | Upgraded 2 | exmple
1 | userid | NULL | downgrade | 0000-01-00 00:00:02 | Downgraded | exmple
1 | userid | NULL | upgrade | 0000-01-00 00:00:03 | Upgraded | exmple
What I would like the outcome to be is:
postid | posttype | target | action | date | title | content
1 | userid | NULL | upgrade | 0000-01-00 00:00:01 | Upgrade 1 | exmple,exmple
1 | userid | NULL | downgrade | 0000-01-00 00:00:02 | Downgraded | exmple
1 | userid | NULL | upgrade | 0000-01-00 00:00:03 | Upgraded | exmple
So as you can see because Upgrade 1 & Upgrade 2 were sent Consecutively, it groups them together. The “Action” table is a reference, and should be used for the consecutive grouping as well as the postid & posttype.
I looked around on SO but didnt see anything quite like mine. Thanks in advance for any help.
Here’s another version that works with MySQL Variables and doesn’t require 3 level nesting deep. The first one pre-sorts the records in order by postID and Date and assigns them a sequential number per group whenever any time a value changes in one of the Post ID, Type and/or action. From that, Its a simple group by… no comparing record version T to T2 to T3… what if you wanted 4 or 5 criteria… would you have to nest even more entries?, or just add 2 more @sql variables to the comparison test…
Your call on which is more efficient…
Here’s my link to SQLFiddle sample
For the title, you might want to adjust the line…
group_concat( distinct PreQuery.Title ) as Titles,
At least this will give DISTINCT titles concatinated… much tougher to get let without nesting this entire query one more level by having the max query date and other elements to get the one title associated with that max date per all criteria.