我有一个像这样的数据库结构:
Car
- id
- carname
- image
- category
- status
Parts
- partid
- partname
CarParts
- carpartid
- carid(fk)
- partid(fk)
- amountid(fk)
Category
- id
- categoryname
Amount
- amountid
- amountvalue
现在正是我正在做的事情:
SELECT * FROM carparts
INNER JOIN car on carparts.carpartid = car.id
INNER JOIN parts on parts.partid = carparts.carpartid
INNER JOIN amount on amount.amountid = carparts.amountid where status = 1
当carparts表中有多个carpart时,返回重复记录。如何将carparts.id和car.id组合成一行,但仍然有多个partname用于该记录?
答案 0 :(得分:0)
试试这个。
SELECT * FROM car left join carparts on car.id = carparts.carpartid
left join parts on parts.partid = carparts.carpartid
left join amount on amount.amountid = carparts.amountid where status = 1
group by car.id
谢谢。
答案 1 :(得分:0)
您可以使用:
SELECT group_concat(parts.partname) as parts FROM car left join carparts on car.id = carparts.carpartid
left join parts on parts.partid = carparts.carpartid
left join amount on amount.amountid = carparts.amountid where status = 1
group by car.id
由于我们需要组合记录,我们是通过group by子句来完成的。此外,由于我们需要在一行中有多个记录,因此我们使用group_concat来连接多个结果。 P.S:group_concat返回的值的数量有一些限制吗?