I looked at other topics in detail about limitations on group_concat () and internal connections, but did not find an answer, so I think I will continue and ask him:
I am developing an existing community photo site. I want to get members who have their birthday on that day (today), and then get each of the 5 most highly rated photos. But I also want only the 10 “most beloved” members of the birthday (ie With the highest number of favorites). Here is what I have:
SELECT users.user_id, users.user_name,
GROUP_CONCAT(CONVERT(photos.photo_id,char(32))
ORDER BY photos.average_rate) as photo_ids
FROM users
INNER JOIN photos ON photos.user_id=users.user_id
WHERE users.day_of_birth = DATE_FORMAT('2012-04-17', '%m-%d')
AND users.photo_count>0
GROUP BY users.user_id
ORDER BY users.favorite_count DESC, users.photo_count DESC LIMIT 0,10
This does what I want, EXCEPT that I cannot limit the number photo_idto 5. This is a problem because the output will be sent as JSON to the application, and some participants have already uploaded over 20,000 photos, which leads to an unacceptably long output line. The only "solution" that seems to work for me is to set the variable group_concat_max_lento something reasonable that will contain at least 5 identifiers, but it is very hacky and unreliable. Is there a way to return exactly 5 photo_idfor a user with a single request? Or do I need to do a loop in my PHP?
I don’t necessarily need photo_id in a comma-separated value, I can also completely exclude the group_concat () method and make an internal join, if possible. But even there I do not know how to limit the results to 5.