MySQL SQL 子句“GROUP BY 1”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7392730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does SQL clause "GROUP BY 1" mean?
提问by Spencer
Someone sent me a SQL query where the GROUP BY
clause consisted of the statement: GROUP BY 1
.
有人向我发送了一个 SQL 查询,其中GROUP BY
子句包含以下语句:GROUP BY 1
.
This must be a typo right? No column is given the alias 1. What could this mean? Am I right to assume that this must be a typo?
这一定是打字错误吧?没有列被赋予别名 1。这意味着什么?我是否正确地认为这一定是一个错字?
回答by Yuck
It means to group by the first column regardless of what it's called. You can do the same with ORDER BY
.
这意味着按第一列分组,不管它叫什么。您可以对ORDER BY
.
回答by mr_eclair
SELECT account_id, open_emp_id
^^^^ ^^^^
1 2
FROM account
GROUP BY 1;
In above query GROUP BY 1
refers to the first column in select statement
which is
account_id
.
在上面的查询GROUP BY 1
中指的是first column in select statement
which is
account_id
。
You also can specify in ORDER BY
.
您也可以在ORDER BY
.
Note : The number in ORDER BY and GROUP BY always start with 1 not with 0.
注意:ORDER BY 和 GROUP BY 中的数字总是以 1 开头,而不是 0。
回答by vol7ron
In addition to grouping by the field name, you may also group by ordinal, or position of the field within the table. 1 corresponds to the first field (regardless of name), 2 is the second, and so on.
除了按字段名称分组外,您还可以按序号或字段在表中的位置分组。1 对应第一个字段(不考虑名称),2 是第二个,依此类推。
This is generally ill-advised if you're grouping on something specific, since the table/view structure may change. Additionally, it may be difficult to quickly comprehend what your SQL query is doing if you haven't memorized the table fields.
如果您根据特定的内容进行分组,这通常是不明智的,因为表/视图结构可能会发生变化。此外,如果您没有记住表字段,可能很难快速理解您的 SQL 查询在做什么。
If you are returning a unique set, or quickly performing a temporary lookup, this is nice shorthand syntax to reduce typing. If you plan to run the query again at some point, I'd recommend replacing those to avoid future confusion and unexpected complications (due to scheme changes).
如果你要返回一个唯一的集合,或者快速执行一个临时查找,这是一个很好的速记语法,可以减少输入。如果您打算在某个时候再次运行查询,我建议您替换它们以避免将来出现混乱和意外的并发症(由于方案更改)。
回答by Daan Geurts
It will group by first field in the select clause
它将按 select 子句中的第一个字段分组
回答by u6582913
That means sql group by 1st column in your select clause, we always use this GROUP BY 1
together with ORDER BY 1
, besides you can also use like this GROUP BY 1,2,3..
, of course it is convenient for us but you need to pay attention to that condition the result may be not what you want if some one has modified your select columns, and it's not visualized
这意味着 sql group by the 1st column in your select 子句,我们总是将 thisGROUP BY 1
与 一起使用ORDER BY 1
,此外你也可以这样使用GROUP BY 1,2,3..
,当然这对我们来说很方便,但你需要注意那个条件,结果可能不是你的想知道是否有人修改了您的选择列,并且没有可视化
回答by wdoering
It will group by the column position you put after the group by clause.
它将按您在 group by 子句之后放置的列位置进行分组。
for example if you run 'SELECT SALESMAN_NAME, SUM(SALES) FROM SALES GROUP BY 1
'
it will group by SALESMAN_NAME
.
例如,如果您运行 ' SELECT SALESMAN_NAME, SUM(SALES) FROM SALES GROUP BY 1
' 它将分组SALESMAN_NAME
。
One risk on doing that is if you run 'Select *
' and for some reason you recreate the table with columns on a different order, it will give you a different result than you would expect.
这样做的一个风险是,如果您运行 ' Select *
' 并且出于某种原因重新创建具有不同顺序的列的表,它将为您提供与您预期不同的结果。