MySQL: GROUP_CONCAT子句中的WHERE条件

huangapple go评论60阅读模式
英文:

MySQL: WHERE in a GROUP_CONCAT clause

问题

我有以下表格:

表格
+--------------------+-------+
| id      |  name    |  age  |
+--------------------+-------+
| 1       | client1  |  10   |
| 2       | client2  |  20   |
| 3       | client3  |  30   |
| 4       | client4  |  40   |
+--------------------+-------+

我尝试运行一个查询,该查询将返回第一行的 id, age,以及除第一行之外的所有行的年龄以逗号分隔的字符串,所以查询的输出应该是:

4, 40, client4, "3,2,1"

我尝试使用以下方式使用 GROUP_CONCAT

SELECT id, age, name, SUBSTRING(GROUP_CONCAT(id), POSITION("," IN GROUP_CONCAT(id ORDER BY id DESC))+1)  as previous_ids
FROM table 
ORDER BY id DESC;

查询结果:

1, 1, client1, "3,2,1"

它完全忽略了外部的 ORDER BY。你有任何修复这个问题的想法吗?或者也许有不同的方法。。

谢谢!

英文:

I've the following table:

table
+--------------------+-------+
| id      |  name    |  age  |
+--------------------+-------+
| 1       | client1  |  10   |
| 2       | client2  |  20   |
| 3       | client3  |  30   |
| 4       | client4  |  40   |
+--------------------+-------+

I'm trying to run a quest that would return the id, age of the first row and a comma delimited string of ages of all rows except the first.

So the output of the query should give:

4, 40, client4, "3,2,1"

I try to use GROUP_CONCAT in the following way:

SELECT id, age, name, SUBSTRING(GROUP_CONCAT(id), POSITION("," IN GROUP_CONCAT(id ORDER BY id DESC))+1)  as previous_ids
FROM table 
ORDER BY id DESC;

Query results:

1, 1, client1, "3,2,1"

It completely ignores the outer ORDER BY.

Any idea how to fix this?? or perhaps a different approach..

Thank you!

答案1

得分: 1

"它完全忽略了外部的ORDER BY

嗯,不对。

您的查询没有太多意义,因为您试图在没有指定应如何聚合它们的情况下对记录进行聚合(没有GROUP BY)。而且,您还应该在输出中排除id,除非它在聚合函数(sum、max、group_concat等)中使用(sum、max、group_concat....)。

一旦您解开了所有这些,您的查询仍然不会按照您的意图进行,因为最后的ORDER BY与GROUP_CONCAT()值排序的顺序无关。您在这里有2个GROUP_CONCAT函数,只有其中一个有自己的ORDER BY子句。

最后,使用2个GROUP_CONCAT表达式有点繁琐。我会选择类似以下的内容...

SELECT ilv.age
, SUBSTRING(ilv.records, POSITION("," IN ilv.records)+1) AS previous
FROM (
  SELECT age
  , GROUP_CONCAT(id ORDER BY id) AS records
  FROM table 
  GROUP BY age
) AS ilv;

"

英文:

> It completely ignores the outer ORDER BY

erm no.

Your query doesn't make a lot of sense as you are trying to aggregate records without specifying how they should be aggregated (no GROUP BY). And you should also be excluding id from the output EXCEPT where it is used within an aggregate function (sum, max, group_concat....).

Once you've untangled all that, your query still isn't going to what you intend because the ORDER BY at the end has no relevance to the order in which the GROUP_CONCAT() values are sorted. You have 2 GROUP_CONCAT functions here, and only one of them has its own ORDER BY clause.

Finally, using the 2 GROUP_CONCAT expressions is somewhat cumbersome. I would have gone with something like...


SELECT ilv.age
, SUBSTRING(ilv.records, POSITION("," IN ilv.records)+1) AS previous
FROM (
  SELECT age
  , GROUP_CONCAT(id ORDER BY id) AS records
  FROM table 
  GROUP BY age
) AS ilv;

huangapple
  • 本文由 发表于 2023年7月20日 21:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定