不了解如何使用GROUP_CONCAT函数,因为我的查询似乎重复了我实际上并不想要的值。
我有三张桌子:
------------------
Content
------------------
cid | title
2334 | Lorem Ipsum
------------------
Organizers
------------------
cid | name | sort
2334 | John Doe | 0
------------------
Participants
------------------
cid | name | sort
2334 | Jane Doe | 0
2334 | Bob Hope | 1
2334 | Bart Simpson | 2
我正在尝试编写一个将产生的查询:
cid | title | organizers | participants
2334 | Lorem Ipsum | John Doe | Jane Doe, Bob Hope, Bart Simpson
所以,我正在尝试使用GROUP_CONCAT。但是,值正在重复,所以我不确定如何输出我正在寻找的结果?
SELECT GROUP_CONCAT(co.name) as organizers,
GROUP_CONCAT(cp.name) FROM content n
LEFT JOIN organizers co ON n.cid = co.cid
LEFT JOIN participants cp ON n.cid = cp.cid
WHERE n.cid = 2334
以上产量:
cid | title | organizers | participants
2334 | Lorem Ipsum | John Doe, John Doe, John Doe | Jane Doe, Bob Hope, Bart Simpson
答案 0 :(得分:2)
select group_concat(distinct co.name)
详细信息:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
<强>语法强>
GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]] [SEPARATOR str_val])
答案 1 :(得分:1)
SELECT n.cid, n.Title,
GROUP_CONCAT(DISTINCT co.name ORDER BY co.sort) as organizers,
GROUP_CONCAT(cp.name ORDER BY cp.sort)
FROM Content n
LEFT JOIN Organizers co ON n.cid = co.cid
LEFT JOIN Participants cp ON n.cid = cp.cid
GROUP BY n.cid