Php 如何避免在使用Sql连接时重复值
我想知道在使用SQLJOIN时如何避免重复值 例如:Php 如何避免在使用Sql连接时重复值,php,mysql,sql,Php,Mysql,Sql,我想知道在使用SQLJOIN时如何避免重复值 例如: 如何从数据库中获取这些值: id => 1 name => Bob img => [water.jpg, beer.jpg] id => 2 name => Junior img => [spoon.jpg] 而不是 id => 1 name => Bob img => water.jpg
如何从数据库中获取这些值:
id => 1
name => Bob
img => [water.jpg, beer.jpg]
id => 2
name => Junior
img => [spoon.jpg]
而不是
id => 1
name => Bob
img => water.jpg
id => 1
name => Bob
img => beer.jpg
id => 2
name => Junior
img => spoon.jpg
我正在使用MySQL对此进行什么查询?您可以使用
groupby
和group\u concat()
:
试试这个:
SELECT users.id, users.name, concat('[',GROUP_CONCAT(images.img SEPARATOR ','),']') as images_concat
FROM users
INNER JOIN images ON (users.id = images.user_id)
GROUP BY users.id;
select u.id, u.name, group_concat(i.img) as imgs
from users u join
images i
on u.id = i.user_id
group by u.id;
SELECT users.id, users.name, concat('[',GROUP_CONCAT(images.img SEPARATOR ','),']') as images_concat
FROM users
INNER JOIN images ON (users.id = images.user_id)
GROUP BY users.id;