gpt4 book ai didi

MySQL GROUP BY "and filter"

转载 作者:可可西里 更新时间:2023-11-01 06:41:15 26 4
gpt4 key购买 nike

假设我有这样的查询:

SELECT name, GROUP_CONCAT(number)
FROM objects
GROUP BY name

它输出:

+----------+----------------------+
| NAME | GROUP_CONCAT(NUMBER) |
+----------+----------------------+
| false_1 | 2,1 |
| false_2 | 3,4 |
| true_1 | 4,3,2,1 |
| true_2 | 2,3 |
+----------+----------------------+

现在如何返回具有 2 和 3 作为 number 的行?

注意:此查询是分组的 - 表有 10 行,如下所示:

+---------+--------+
| NAME | NUMBER |
+---------+--------+
| true_1 | 1 |
| true_1 | 2 |
| true_1 | 3 |
| ... | ... |
+---------+--------+

[Link to SQLFiddle]

最佳答案

SELECT name, GROUP_CONCAT(number)
FROM objects
WHERE number IN (2,3)
GROUP BY name
HAVING COUNT(*) = 2

或者如果您想保留名称所具有的所有值(value),

SELECT  a.name, GROUP_CONCAT(A.number)
FROM objects a
INNER JOIN
(
SELECT name
FROM objects
WHERE number IN (2,3)
GROUP BY name
HAVING COUNT(*) = 2
) b ON a.Name = b.Name
GROUP BY a.name

关于MySQL GROUP BY "and filter",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13308281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com