从查询中选择多个值

时间:2016-06-20 08:15:31

标签: mysql sql

我有一个如下所示的查询:

SELECT T1.ID, T3.Val FROM Table1 as T1
INNER JOIN Table2 as T2 ON T1.ID = T2.ID
INNER JOIN Table3 as T3 ON T2.ID = T3.ID

我的问题是最后一次内连接返回3行,我想打印所有这些值,如T3.val1,T3.val2,T3.val3

这是怎么做到的?

编辑:示例数据: T3

DataID, DefinitionID, ItemID, UnitID, Val,    TS
123     111           4541    45545   0.05    2016-05-07 06:14:07
124     111           5487    69587   0.026   2016-05-07 09:11:01
125     111           3621    12862   0       2016-05-07 10:04:17

这是我的查询:

SELECT t1.EventID, group_concat(t3.Val) FROM T1 as t1
INNER JOIN T2 as t2 ON t1.EventID = t2.EventID
INNER JOIN T3 as t3 on t2.DefinitionID = t3.DefinitionID
GROUP BY t3.Val;

结果是:

EventID   group_concat(t3.val)
3         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0........

1 个答案:

答案 0 :(得分:0)

SELECT T1.ID, GROUP_CONCAT(T3.Val,',') FROM Table1 as T1
INNER JOIN Table2 as T2 ON T1.ID = T2.ID
INNER JOIN Table3 as T3 ON T2.ID = T3.ID
GROUP BY T3.ID
Table1
---------------------
ID
1

Table2
---------------------
ID
1


Table3
---------------------
ID    Val
1      A
1      B
1      C

<强>输出

ID   Val
1    A,B,C