简体   繁体   English

如何将相同的id和mrege数据组合成一个记录?

[英]How to combine the same id and mrege the data into one record?

I have a database structure like so: 我有一个像这样的数据库结构:

Car
 - id   
 - carname
 - image
 - category
 - status

Parts
 - partid
 - partname

CarParts
 - carpartid
 - carid(fk)
 - partid(fk)
 - amountid(fk)

Category
 - id
 - categoryname

Amount
 - amountid
 - amountvalue

Right now this is what I am working on: 现在这就是我正在做的事情:

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

This returns duplicate records when there is more than one carpart in the carparts table. 当carparts表中有多个carpart时,这将返回重复记录。 How can I combine the carparts.id and car.id to one row but still have multiple partname for that one record? 如何将carparts.id和car.id组合成一行,但仍然有一个记录的多个partname?

Try this. 尝试这个。

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

Thank you. 谢谢。

You can use : 您可以使用 :

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

Since we need to combine records, we are doing it via group by clause. 由于我们需要组合记录,因此我们通过group by子句进行组合。 Also since we need multiple records in one row, we are using group_concat to concatenate multiple results. 此外,由于我们需要在一行中有多个记录,因此我们使用group_concat来连接多个结果。 PS : There is some limitation on number of values returned by group_concat ? PS:group_concat返回的值有一些限制吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM