ORDER BY之后的GROUP BY [英] GROUP BY after ORDER BY

查看:127
本文介绍了ORDER BY之后的GROUP BY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  SELECT 
`pages`.```,
`content`.`id_language`,
[...]

[...]

ORDER BY
FIND_IN_SET(`languages`。 ''''''')DESC

[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 ,因为它不是一个规则。

解决方案

< pre $ 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


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.

解决方案

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

这篇关于ORDER BY之后的GROUP BY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆