SQL Group By with limit and distinct - MySQL
This is my tables and structure
Shoutbox
- SID //PK
- Title //Title
shoutbox_follower
- SID
- UID //User ID
Shouts
- SID
- Text //Shouts
I want to get the last 1 (latest) shouts(Shouts.Text) for each Shoutbox, which the user is following
i tried this but it did not worked
select shoutbox_follower.SID,shoutbox.title, shouts.text from shoutbox_follower, s开发者_如何学Pythonhoutbox, shouts where shoutbox_follower.uid=1;
But i can do the work with multiple query
SELECT SID from shoutBox_Follower where UID=5
SELECT SID,TEXT from Shouts where SID in ($boxList) order by id desc limit 1
SELECT Title from Shoutbox where SID=? limit 1
You can combine the queries, in cases like this:
SELECT SID,
(select TEXT
from Shouts
where Shouts.SID = shoutBox_Follower.SID
order by Shouts.SID desc limit 1
) as shout_text
from shoutBox_Follower
where UID=5
It performs very fast for small numbers of rows (assuming you've the needed indexes, of course). Based on your third query, the title can be fetched using a simple inner join.
In comments you mentioned that you need fast solution. If so - then add a field latest_shout_id
to the shoutbox
table and maintain it with AFTER INSERT
trigger of shouts
table. That's all.
Here is the query that will do what you want (assuming you have shout_id
as a PK in shouts
table and reference to shoutbox
by shoutbox_id
field):
SELECT x.*
FROM shoutbox_follower f
INNER JOIN (SELECT MAX(shout_id) shout_id,
shoutbox_id
FROM Shouts s
GROUP BY shoutbox_id) x ON x.shoutbox_id = f.sid
WHERE f.uid = 5
Changing it to correlated subquery may change it in faster or slower manner, it depends on a lot of factors.
精彩评论