ORDER BY 之后的 GROUP BY
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.
您可能需要在原始查询中添加一个 GROUP BY 列,以及当前分组依据的任何列。该列在分组后可用于随后进行排序。例如:
我希望 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:
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.
非常有趣,尝试一下
据我所知,规则集是
id_language=3
,这与使用
where
没有区别Very amusing, try
As far I can tell, the rule set is
id_language=3
,which make no differences from using
where