有条件地限制 mySQL 的结果数量?
这是我的查询:
SELECT * FROM Photos WHERE Event_ID IN ($eventidstring)
我知道我可以使用 LIMIT 5 限制此查询的结果总数
我需要限制 $eventidstring 中每个值的结果数量。
所以如果 $eventidstring = 23,41,23*
*并且有 10 个结果,其中 Event_ID = 23,我想将此数量限制为 5。$eventidstring 中的所有其他值都相同。
Here is my query:
SELECT * FROM Photos WHERE Event_ID IN ($eventidstring)
I know I can limit the total amount of results from this query using LIMIT 5
I need the Limit the amount of results Per value in $eventidstring.
So if $eventidstring = 23,41,23*
*And there are 10 results WHERE Event_ID = 23, I want to limit this amount to 5. The same for all the other values in $eventidstring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能会喜欢在 MySQL 中执行类似于 Oracle 的 RANK by PARITION 的操作。
遗憾的是,此功能在 MySQL 中不可用,尽管您可以使用此方法来解决它
将其转储到内联视图中,然后选择排名 <= 5 的行;
因此:
考虑一下您将如何通过 Event_Id“选择”前五个。您始终可以在
ORDER BY p.Event_Id DESC
之后添加更多内容来决定这一点。You may have some joy doing something similar to Oracle's RANK by PARITION in MySQL.
Sadly this feature is not available in MySQL though you can work around it using this method
Dump that in an inline view and then select those rows with rank <= 5;
Hence:
Consider how you are going to 'choose' the top five by Event_Id too. You can always add in more after the
ORDER BY p.Event_Id DESC
to decide this.我认为您正在 PHP 内部的某个位置编写该查询,因此您需要将
$eventidstring
拆分为其组件值,为每个值形成一个 SELECT,并在第一个值之后将所有值合并。您应该使用某种循环来执行此操作,并在循环中连接查询字符串......
I take it you're writing that query somewhere inside your PHP, so you need to split the
$eventidstring
into it's component values, form a SELECT for each and UNION all after the first one.You sould do this with a loop of some sort, and concatenate the query strings in the loop...
如果我理解正确并且您想要每个获得五个,您可以使用以下内容:
If I understand correctly and you want to get five of each, you can use this:
也许使用 SELECT UNION 但您需要为每个值进行新的选择:
Maybe with a SELECT UNION but you need a new select for each value: