MYSQL连接两个表并合并行值
我有一个MySQL数据库,有两个表: 表1:MYSQL连接两个表并合并行值,mysql,join,concat,Mysql,Join,Concat,我有一个MySQL数据库,有两个表: 表1: Product_ID | Product_Name | Product_Description ------------------------------------------------ 1 | Coca-Cola | Coke description. 2 | Pepsi | Pepsi description. 3 | Fant
Product_ID | Product_Name | Product_Description
------------------------------------------------
1 | Coca-Cola | Coke description.
2 | Pepsi | Pepsi description.
3 | Fanta | Fanta description
...
表2:
Product_ID | Product_SKU | Product_Size | Product_Price
------------------------------------------------
1 | COKE330 | 330ml | 0.79
1 | COKE500 | 500ml | 1.29
2 | PEPS330 | 330ml | 0.59
2 | PEPS500 | 500ml | 0.99
...
我希望查询数据库并将这两个表连接在一起,但合并具有相同产品id的重复行,以便返回的结果如下所示:
Product_ID | Product_Name | Product_Sizes | Product_Prices | Product_Description
---------------------------------------------------------------------------------
1 | Coca-Cola | 330ml, 500ml | 0.79, 1.29 | Coke description.
2 | Pepsi | 330ml, 500ml | 0.59, 0.99 | Pepsi description.
...
提前感谢您可以对行进行分组,并使用
group_CONCAT()
组合分组值,如所示:
select
a.product_id,
max(a.product_name),
group_concat(b.product_size) as product_sizes,
group_concat(b.product_price) as product_prices,
a.product_description
from table1 a
join table2 b on b.product_id = a.product_id
group by a.product_id
您尝试过查询了吗?请您发布插入和创建脚本,以便能够轻松分析和正确回答?