我有一张这样的桌子:
fkey | sensor | depth | value
-----+--------+-------+-------
1 | 1 | 1 | 34
1 | 1 | 2 | 27
1 | 2 | 1 | 22
1 | 2 | 2 | 34
1 | 2 | 3 | 56
2 | 1 | 1 | 12
2 | 1 | 2 | 24
2 | 2 | 1 | 56
3 | 1 | 1 | 43
3 | 1 | 2 | 89
3 | 1 | 3 | 97
如何将select查询写入每个传感器的
GROUP_CONCAT
值和ORDER BY
深度以显示此信息?fkey | sensor1_values | sensor2_values
-----+-----------------+------------------
1 | 34,27 | 22,34,56
2 | 12,24 | 56
3 | 43,89,97 | NULL
最佳答案
尝试将GROUP_CONCAT
与CASE
表达式一起使用,以针对每个传感器数据。
SELECT
fkey,
GROUP_CONCAT(CASE WHEN sensor=1 THEN value END ORDER BY depth) AS sensor1_values,
GROUP_CONCAT(CASE WHEN sensor=2 THEN value END ORDER BY depth) AS sensor2_values
FROM yourTable
GROUP BY fkey;