SQL查询一对多返回一条数据

时间:2024-03-23 10:09:01
查询中product和product_img是一对多的关系(数据库是MySQL)
select p.id,
       p.product_cost_price,
       p.product_trade_price,
       p.product_sale_price,
       p.collect_num,
       p2.collect_status,
       p2.ID as collect_id,
       p3.name,
       GROUP_CONCAT(p1.img_path SEPARATOR '|') as imgPath
from dn_product as p
       left join dn_product_img as p1 on p.id = p1.product_id
       left join dn_product_collect as p2 on p.id = p2.product_id
       left join dn_person as p3 on p3.ID = p2.person_id
group by p.ID
limit 10

SQL查询一对多返回一条数据