如何在同一查询中获取分组和排序 [英] how to get group by and order by in same query

查看:158
本文介绍了如何在同一查询中获取分组和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在同一查询中获取分组和排序

how to get group by and order by in same query

推荐答案

您可以使用以下查询



You can use the below query

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
          select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    ]


您可以使用GROUP BY和ORDER BY非常容易,但是......只有当你按照GROUP中的一个列进行排序时:

You can use GROUP BY and ORDER BY very easily, but...only if you are ordering by one of the columns in the GROUP:
SELECT Title FROM MyTable
GROUP BY Title
ORDER BY Title DESC

如果要ORDER BY的列不在GROUP中,那么这意味着它可以为GROUP BY将返回的每一行提供多个值,并且SQL无法排序基于此 - 因为它不知道它应该用于排序的哪个值。

If the column you want to ORDER BY isn't in the GROUP, then that means it can have multiple values for each row the GROUP BY will return and SQL can't sort based on that - because it doesn't know which of the values it should use for sorting.






我给我的解决方案我的想法,





SQL>



从Table_name中选择column_name

group by column_name

order by column_name1
Hi,

I am giving my solution what i think,


SQL>

Select column_name from Table_name
group by column_name
order by column_name1


这篇关于如何在同一查询中获取分组和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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