避免在Mysql左连接查询中重复

时间:2017-05-09 07:40:52

标签: mysql join left-join

我必须从三个表中获取数据,例如Userdetails,tasks,timedetails,所以我获取数据,但它是基于timedetails表重复的。例如,人物ID是 - 1.它在timedetails表中呈现两次,然后我得到重复的行。 我的查询是

SELECT  GROUP_CONCAT(B.task_status) as task_status,
        GROUP_CONCAT(B.task_type) as task_type,
        GROUP_CONCAT(B.task_id) as task_id,
        GROUP_CONCAT(B.task_name) as task_name,
        A.us_id,
        A.us_name,
        C.out_time
FROM    ts_userdetails A
LEFT JOIN
        cms_task B
ON      B.emp_id = A.us_id
LEFT JOIN
        ts_timedetails C
ON      C.user_id=A.us_id
WHERE   C.entry_date='2017-05-09' AND
        A.us_id!='1'
GROUP BY C.user_id

我得到的结果就像 enter image description here

我不希望在显示的字段中出现重复的内容。

如果我有一个特定人的2个时间序列,则表示重复发生2次。我只想要一次。

1 个答案:

答案 0 :(得分:0)

使用distinct关键字

    SELECT GROUP_CONCAT(distinct B.task_status) as task_status,GROUP_CONCAT(distinct B.task_type) as task_type,GROUP_CONCAT(distinct B.task_id) as task_id,GROUP_CONCAT(distinct B.task_name) as task_name,A.us_id,A.us_name,C.out_time FROM ts_userdetails A LEFT JOIN cms_task B ON B.emp_id = A.us_id LEFT JOIN ts_timedetails C ON C.user_id=A.us_id WHERE C.entry_date='2017-05-09' AND A.us_id!='1' GROUP BY C.user_id