SQL group_concat删除左联接

时间:2018-10-12 15:06:11

标签: java sql group-concat

我有这两个表

 FunctionName | value
 -------------+---------
 intensity    |  0
 status       |  NULL

 FunctionName | StatusName 
 -------------+------------
 status       |  ON        
 status       |  Off         

我正在使用以下查询:

SELECT 
    Functions.FunctionName, Functions.value,
    GROUP_CONCAT(FunctionsWithStatus.StatusName)
FROM
    Functions
LEFT JOIN
    FunctionsWithStatus ON Functions.FunctionName = FunctionsWithStatus.Functionsname

结果是:

Name   | value | group_concat
status | 0     | off,on

我还如何检索“强度”的值并得到如下结果:

Name      | value | group_concat
intensity |  0    |  NUll
status    |  0    | off,on

2 个答案:

答案 0 :(得分:0)

添加Group by应该完成任务:

SELECT Functions.FunctionName,Functions.value,group_concat(FunctionsWithStatus.StatusName)
from  Functions
left join  FunctionsWithStatus on Functions.FunctionName = FunctionsWithStatus.Functionsname
Group by Functions.FunctionName

答案 1 :(得分:0)

您的查询格式不正确。您将未聚合的列与聚合的列混合在一起。 MySQL将其视为不带group by的聚合查询-它仅返回一行。未汇总列的值来自不确定的行。

您应该在group by中包括所有未聚合的列(不仅是一个好主意,而且是标准的SQL):

select f.FunctionName, f.value, group_concat(fws.StatusName)
from Functions f left join
     FunctionsWithStatus fws
     on f.FunctionName = fws.Functionsname
group by f.FunctionName, f.value;
相关问题