1到多个选择和连续

时间:2016-06-14 23:22:55

标签: mysql sql

我有两张桌子:

[customer]
id
name

[customer_photo]
id
customer_id
photo

我想选择所有客户及其照片。

此查询正在执行此操作,但只获取至少拥有一张照片的用户:

SELECT customer.id, name, GROUP_CONCAT(cp.photo) as photos 
FROM customer 
JOIN customer_photo cp ON cp.customer_id = customer.id 
GROUP BY customer.id

我希望得到所有用户,即使他们没有照片。

1 个答案:

答案 0 :(得分:1)

只需添加LEFT加入

即可
SELECT customer.id, name, GROUP_CONCAT(cp.photo) as photos 
FROM customer 
LEFT JOIN customer_photo cp ON cp.customer_id = customer.id 
GROUP BY customer.id