ORDER BY 之后的 GROUP BY

发布于 2024-12-03 04:38:28 字数 541 浏览 1 评论 0原文

我需要在 ORDER BY 之后执行 GROUP BY 。我不明白为什么 MySQL 不支持这一点。 这是我的代码:

SELECT
    `pages`.`id`,
    `contents`.`id_language`,
    [...]

[...]

ORDER BY
    FIND_IN_SET(`languages`.`id`, '3') DESC

[the GROUP BY]

结果将是这样的:

id | id_language | ...
1    3
1    1
2    3
2    5
2    1

我需要按 ID 分组,我只需要第一个结果,并且需要保存在视图中。因此我不能使用 SUBQUERY。

结果必须是:

id | id_language | ...
1    3
2    3

注意:不要对 id_language = 3 感到困惑,因为它不是规则。

I need to do GROUP BY after ORDER BY. I don't understand why MySQL doesn't support that.
This is my code:

SELECT
    `pages`.`id`,
    `contents`.`id_language`,
    [...]

[...]

ORDER BY
    FIND_IN_SET(`languages`.`id`, '3') DESC

[the GROUP BY]

The results will be something like this:

id | id_language | ...
1    3
1    1
2    3
2    5
2    1

I need to group by ID, I need only the first result and I need to save in a view. I can't use a SUBQUERY because of that.

The result need to be:

id | id_language | ...
1    3
2    3

Note: Don't get confused by id_language = 3, because it isn't a rule.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

隔岸观火 2024-12-10 04:38:28
SELECT id, idl
FROM (SELECT
    `pages`.`id` as id,
    `contents`.`id_language` as idl,
    [...]

[...]

ORDER BY
    FIND_IN_SET(`languages`.`id`, '3') DESC
     ) d
GROUP BY d.id
SELECT id, idl
FROM (SELECT
    `pages`.`id` as id,
    `contents`.`id_language` as idl,
    [...]

[...]

ORDER BY
    FIND_IN_SET(`languages`.`id`, '3') DESC
     ) d
GROUP BY d.id
不美如何 2024-12-10 04:38:28

Group By会对结果集进行分组,一般用于聚合。
Order By 是对结果进行排序的方式。

Group By will group result sets, and is generally used for aggregation.
Order By is the way that results are sorted.

久光 2024-12-10 04:38:28

您可能需要在原始查询中添加一个 GROUP BY 列,以及当前分组依据的任何列。该列在分组后可用于随后进行排序。例如:

SELECT
    SUM(IF(`languages`.`id` = 3, 1, 0)) AS languageOrder,
    `pages`.`id`,
    `contents`.`id_language`,
    [...]

[...]

[GROUP BY...]

ORDER BY languageOrder DESC

我希望 languageOrder 对于包含语言 #3 的组为正,否则为 0。因此包含语言 3 的组将位于顶部。

You may want an additional column in your original query that you GROUP BY, along with whatever you're currently grouping by. That column, when grouped, could then be used to order afterward. For instance:

SELECT
    SUM(IF(`languages`.`id` = 3, 1, 0)) AS languageOrder,
    `pages`.`id`,
    `contents`.`id_language`,
    [...]

[...]

[GROUP BY...]

ORDER BY languageOrder DESC

I would intend for languageOrder to be positive for groups that contain language #3, 0 otherwise. So groups that contain language 3 will be at the top.

等待我真够勒 2024-12-10 04:38:28

非常有趣,尝试一下

select * from your_table
where id_language=3
order by id;

据我所知,规则集是id_language=3
这与使用 where 没有区别

Very amusing, try

select * from your_table
where id_language=3
order by id;

As far I can tell, the rule set is id_language=3,
which make no differences from using where

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