假设我有一个这样的表:
+----+----------+
|name|testValue |
+----+----------+
|A |3 |
+----+----------+
|A |4 |
+----+----------+
|A |7 |
+----+----------+
|B |0 |
+----+----------+
|B |3 |
+----+----------+
|C |5 |
+----+----------+
|C |5 |
+----+----------+
|C |6 |
+----+----------+
Run Code Online (Sandbox Code Playgroud)
现在我首先要按名称对列进行分组,然后只能检索任何行满足特定条件的组.对于每个组,我想要检索所有"testValues",我目前正在使用GROUP_CONCAT.
例如,如果我想要检索任何"testValues"在4到8之间的所有组:
SELECT
name,
testValue,
GROUP_CONCAT(testValue SEPARATOR '#') AS testValues
FROM myTable
GROUP BY name
HAVING testValue > 4 AND testValue < 8
Run Code Online (Sandbox Code Playgroud)
但这只会检查组中第一行的"testValue"是否匹配.我在这个例子中的预期输出是:
+----+---------+----------+
|name|testValue|testValues|
+----+---------+----------+
|A |3 |3#4#7 |
+----+---------+----------+
|C |5 |5#5#6 |
+----+---------+----------+
Run Code Online (Sandbox Code Playgroud)
虽然我的示例查询的实际输出是:
+----+---------+----------+
|name|testValue|testValues|
+----+---------+----------+
|C |5 …Run Code Online (Sandbox Code Playgroud)