Php mysql查询以避免重复结果
我有多个表,如下所示: 表1:产品Php mysql查询以避免重复结果,php,mysql,Php,Mysql,我有多个表,如下所示: 表1:产品 +-----+----------+--------+---------------------+ | id | biz_id | name | message | +-----+----------+--------+---------------------+ | 1 | 1 | test1 | One tow three | | 2 | 1 | test1 | One
+-----+----------+--------+---------------------+
| id | biz_id | name | message |
+-----+----------+--------+---------------------+
| 1 | 1 | test1 | One tow three |
| 2 | 1 | test1 | One tow three |
| 3 | 1 | test1 | One tow three |
| 4 | 2 | test2 | hello world |
| 5 | 2 | test2 | hello world |
+-----+----------+--------+---------------------+
表2:图像
+-----+----------+--------------+-------------------+
| id | biz_id | product_id | path |
+-----+----------+--------------+-------------------+
| 1 | 1 | 1 | img/qwert1.jpg |
| 2 | 1 | 2 | img/qwert2.jpg |
| 3 | 1 | 3 | img/qwert3.jpg |
| 4 | 2 | 4 | img/qwery4.jpg |
| 5 | 2 | 5 | img/qwert5.jpg |
+-----+----------+--------------+-------------------+
在连接多个表时,如何避免mysql中的重复
我的查询是连接这两个表,以便避免重复的产品(按名称获取Distint product),并获取与该产品相关联的所有图像(例如product>name-test1具有图像qwert1.jpg、qwert2.jpg、qwert2.jpg)
请注意,这不是标准的SQL,但大多数DBMS都提供此功能(尽管关键字可能有点不同)。简单连接应该可以完成所有事情
select p.name as Name, i.message as IMG from Images
left join Product p
on i.biz_id=p.biz_id
group by p.name;
使用
左连接
与分组_CONCAT
或分组方式
select p.name name,GROUP_CONCAT(i.path) img
from Images i left join Product p
on i.biz_id=p.biz_id
group by i.product_id
期望的结果是什么样的?
select p.name name,GROUP_CONCAT(i.path) img
from Images i left join Product p
on i.biz_id=p.biz_id
group by i.product_id