GROUP_CONCAT()不能单独使用?
GROUP_CONCAT() can't be used by itself?
我有 table 个产品,想要 select 所有 ID 作为逗号分隔的字符串。当我 运行 以下查询时:
SELECT GROUP_CONCAT(p.id SEPARATOR ',') FROM products p GROUP BY 1
我收到错误:
Can't group on 'GROUP_CONCAT(p.id SEPARATOR ',')'
但是,如果我将查询更改为:
SELECT id, GROUP_CONCAT(p.id SEPARATOR ',') FROM products p GROUP BY 1
然后它 returns 每个产品一行有两列,每列都有一个匹配的产品 ID。这不是我想要的,但我显示此查询是为了证明 select 添加一个额外的列会导致错误消失。
为什么GROUP_CONCAT()不能自己使用?
当您使用时:
SELECT id, GROUP_CONCAT(p.id SEPARATOR ',') FROM products p GROUP BY 1
您正在按 ID 分组
如果这是你想要的,你可以试试:
SELECT GROUP_CONCAT(p.id SEPARATOR ',') FROM products p GROUP BY id
如果您只需要一行,所有 ID 都用“,”分隔,您只需要:
SELECT GROUP_CONCAT(id) FROM products
如果你想将 所有 找到的行组合成一个,那么你根本不需要 GROUP BY
子句。
来自docs:
If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.
你应该可以做到:
SELECT GROUP_CONCAT(p.id SEPARATOR ',') FROM products p
请注意,SEPARATOR ','
是 默认值 ,您可以只使用:
SELECT GROUP_CONCAT(p.id) FROM products p
根据 SELECT
语句的 docs,您可以将 position 传递给 GROUP BY
,它将按该列分组.
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column names, column aliases, or column positions. Column positions are integers and begin with 1 [...] Use of column positions is deprecated because the syntax has been removed from the SQL standard.
因此,GROUP BY 1
被解释为 GROUP BY GROUP_CONCAT(p.id SEPARATOR ',')
,这是没有意义的。
只需这样做:
create table products (id int);
insert into products values (1), (2), (3);
select group_concat(id separator ',') from products;
结果:
+--------------------------------+
| group_concat(id separator ',') |
+--------------------------------+
| 1,2,3 |
+--------------------------------+
要使 GROUP BY 正常工作,您需要将分组依据的列包含在所选字段中。当你写 GROUP By 1 时,它实际上是按第一列进行分组。
如果您只想对所有行进行分组(而不是按任何列),您可以使用对所有行都相同的虚拟列。像这样:
SELECT 'test', GROUP_CONCAT(p.id SEPARATOR ',') FROM products p GROUP BY 1
或者完全删除 GROUP BY
SELECT GROUP_CONCAT(p.id SEPARATOR ',') FROM products p
我有 table 个产品,想要 select 所有 ID 作为逗号分隔的字符串。当我 运行 以下查询时:
SELECT GROUP_CONCAT(p.id SEPARATOR ',') FROM products p GROUP BY 1
我收到错误:
Can't group on 'GROUP_CONCAT(p.id SEPARATOR ',')'
但是,如果我将查询更改为:
SELECT id, GROUP_CONCAT(p.id SEPARATOR ',') FROM products p GROUP BY 1
然后它 returns 每个产品一行有两列,每列都有一个匹配的产品 ID。这不是我想要的,但我显示此查询是为了证明 select 添加一个额外的列会导致错误消失。
为什么GROUP_CONCAT()不能自己使用?
当您使用时:
SELECT id, GROUP_CONCAT(p.id SEPARATOR ',') FROM products p GROUP BY 1
您正在按 ID 分组
如果这是你想要的,你可以试试:
SELECT GROUP_CONCAT(p.id SEPARATOR ',') FROM products p GROUP BY id
如果您只需要一行,所有 ID 都用“,”分隔,您只需要:
SELECT GROUP_CONCAT(id) FROM products
如果你想将 所有 找到的行组合成一个,那么你根本不需要 GROUP BY
子句。
来自docs:
If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.
你应该可以做到:
SELECT GROUP_CONCAT(p.id SEPARATOR ',') FROM products p
请注意,SEPARATOR ','
是 默认值 ,您可以只使用:
SELECT GROUP_CONCAT(p.id) FROM products p
根据 SELECT
语句的 docs,您可以将 position 传递给 GROUP BY
,它将按该列分组.
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column names, column aliases, or column positions. Column positions are integers and begin with 1 [...] Use of column positions is deprecated because the syntax has been removed from the SQL standard.
因此,GROUP BY 1
被解释为 GROUP BY GROUP_CONCAT(p.id SEPARATOR ',')
,这是没有意义的。
只需这样做:
create table products (id int);
insert into products values (1), (2), (3);
select group_concat(id separator ',') from products;
结果:
+--------------------------------+
| group_concat(id separator ',') |
+--------------------------------+
| 1,2,3 |
+--------------------------------+
要使 GROUP BY 正常工作,您需要将分组依据的列包含在所选字段中。当你写 GROUP By 1 时,它实际上是按第一列进行分组。
如果您只想对所有行进行分组(而不是按任何列),您可以使用对所有行都相同的虚拟列。像这样:
SELECT 'test', GROUP_CONCAT(p.id SEPARATOR ',') FROM products p GROUP BY 1
或者完全删除 GROUP BY
SELECT GROUP_CONCAT(p.id SEPARATOR ',') FROM products p