[英]Limit results on an GROUP_CONCAT() or INNER JOIN
I've perused extensively the other threads talking about limits on group_concat() and inner joins but haven't found my answer, so I guess I'll go ahead and ask it: 我已经广泛地阅读了其他关于group_concat()和内部联接的限制的线程但是没有找到我的答案,所以我想我会继续问它:
I'm developing an existing photo community site. 我正在开发一个现有的照片社区网站。 I want to retrieve members who have their birthday on a given day (today) and then retrieve each member's 5 most highly rated photos.
我想检索在某一天(今天)过生日的会员,然后检索每个会员的5张评价最高的照片。 But I also only want the 10 "most favorite" birthday members (ie with the highest favorite count).
但我也只想要10个“最喜欢的”生日会员(即最喜欢的人数)。 Here's 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 amount of photo_id
s to 5. This is a problem since the output will be sent as JSON to the app, and some members have uploaded upwards of 20,000 photos already, leading to an unacceptably long output string. 这是我想要的,除了我不能将
photo_id
的数量限制为5.这是一个问题,因为输出将作为JSON发送到应用程序,并且一些成员已经上传了超过20,000张照片,导致不可接受长输出字符串。 The only "solution" that seems to work for me is setting the sever variable group_concat_max_len
to something reasonable that will hold at least 5 ids, but this is very hacky and unreliable. 似乎对我
group_concat_max_len
的唯一“解决方案”是将服务器变量group_concat_max_len
设置为合理的,至少可以容纳5个ID,但这非常hacky且不可靠。 Is there any way to return exactly 5 photo_id
s per user with a single query? 有没有办法通过一个查询返回每个用户5个
photo_id
? Or will I need to do a loop in my PHP? 或者我需要在PHP中循环吗?
I don't necessarily need the photo_ids in a comma-separated value, I can also ditch the group_concat() approach entirely and do an inner join if that is more feasible. 我不一定需要以逗号分隔值的photo_id,我也可以完全抛弃group_concat()方法,如果更可行则进行内连接。 But even there I'm not aware of a way to limit the results to 5.
但即使在那里,我也没有意识到将结果限制为5的方法。
These advanced ones are what makes me love MySQL :) 这些先进的是让我爱MySQL的原因:)
SELECT user_id, user_name,
GROUP_CONCAT(CONVERT(photo_id, char(32)) ORDER BY photos.average_rate) as photo_ids
FROM ( SELECT user_id, user_name, photo_id, favorite_count, photo_count,
(case when @user_id = user_id then @rownum := @rownum + 1 else CONCAT(@rownum := 1, @user_id := user_id) end) AS dummy_val
FROM ( SELECT users.user_id, users.user_name, users.favorite_count, users.photo_count, photos.photo_id
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
ORDER BY users.id ASC, photos.average_rate ASC
) AS h,
( @rownum := NULL,
@user_id := NULL
) AS vars
HAVING rownum <= 5) AS h2
GROUP BY user_id
ORDER BY favorite_count DESC, photo_count DESC LIMIT 0, 10
Basicly I get all rows, and throw away all photos which are 6 or higher in calculated rownum. 基本上我得到所有行,并丢弃计算出的rownum中6或更高的所有照片。
SELECT u.user_id
, u.user_name
, GROUP_CONCAT(p.photo_id ORDER BY p.average_rate) AS photo_ids
FROM
( SELECT user_id
, user_name
, favorite_count
, photo_count
FROM users
WHERE day_of_birth = DATE_FORMAT('2012-04-17', '%m-%d')
AND photo_count > 0
ORDER BY favorite_count DESC
, photo_count DESC
LIMIT 10
) AS u
INNER JOIN
photos AS p
ON p.user_id = u10.user_id
AND p.average_rate >=
( SELECT pp.average_rate
FROM photos AS pp
WHERE pp.user_id = u10.user_id
ORDER BY pp.average_rate DESC
LIMIT 1 OFFSET 4
)
GROUP BY u.user_id
ORDER BY u.favorite_count DESC
, u.photo_count DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.