有条件地限制 mySQL 的结果数量?

发布于 2024-12-12 03:27:12 字数 341 浏览 0 评论 0原文

这是我的查询:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

看海 2024-12-19 03:27:12

您可能会喜欢在 MySQL 中执行类似于 Oracle 的 RANK by PARITION 的操作。

遗憾的是,此功能在 MySQL 中不可用,尽管您可以使用此方法来解决它

将其转储到内联视图中,然后选择排名 <= 5 的行;

因此:

SELECT t.* FROM (
SELECT ( 
        CASE Event_id 
        WHEN @curEventId 
        THEN @curRow := @curRow + 1 
        ELSE @curRow := 1 AND @curEventId := Event_Id END
      ) AS rank,
      p.*
FROM      Photos p INNER JOIN (SELECT @curRow := 0, @curEventId := '') r
ORDER BY p.Event_Id DESC
) t
WHERE t.rank <= 5 ORDER BY t.Event_Id asc;

考虑一下您将如何通过 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:

SELECT t.* FROM (
SELECT ( 
        CASE Event_id 
        WHEN @curEventId 
        THEN @curRow := @curRow + 1 
        ELSE @curRow := 1 AND @curEventId := Event_Id END
      ) AS rank,
      p.*
FROM      Photos p INNER JOIN (SELECT @curRow := 0, @curEventId := '') r
ORDER BY p.Event_Id DESC
) t
WHERE t.rank <= 5 ORDER BY t.Event_Id asc;

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.

几味少女 2024-12-19 03:27:12

我认为您正在 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...

故事与诗 2024-12-19 03:27:12

如果我理解正确并且您想要每个获得五个,您可以使用以下内容:

(SELECT * FROM Photos WHERE Event_ID = 23 LIMIT 5)
UNION
(SELECT * FROM Photos WHERE Event_ID = 41 LIMIT 5)
UNION
(SELECT * FROM Photos WHERE Event_ID = ... LIMIT 5)
...

If I understand correctly and you want to get five of each, you can use this:

(SELECT * FROM Photos WHERE Event_ID = 23 LIMIT 5)
UNION
(SELECT * FROM Photos WHERE Event_ID = 41 LIMIT 5)
UNION
(SELECT * FROM Photos WHERE Event_ID = ... LIMIT 5)
...
他是夢罘是命 2024-12-19 03:27:12

也许使用 SELECT UNION 但您需要为每个值进行新的选择:

SELECT * FROM Photos WHERE Event_ID = 23 LIMIT 5
UNION SELECT * FROM Photos WHERE Event_ID = 41 LIMIT 5
UNION SELECT ...

Maybe with a SELECT UNION but you need a new select for each value:

SELECT * FROM Photos WHERE Event_ID = 23 LIMIT 5
UNION SELECT * FROM Photos WHERE Event_ID = 41 LIMIT 5
UNION SELECT ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文