我有一个带有两个表的MySQL数据库:
表1:
import numpy as np
divisor_list=[3,5,7,9,11,15,18]
divisible_numbers=np.array(list(range(100)))
divisible_numbers=divisible_numbers[(divisible_numbers.reshape(-1,1)%divisor_list==0).any(axis=1)]
>>> divisible_number
[ 0 3 5 6 7 9 10 11 12 14 15 18 20 21 22 24 25 27 28 30 33 35 36 39
40 42 44 45 48 49 50 51 54 55 56 57 60 63 65 66 69 70 72 75 77 78 80 81
84 85 87 88 90 91 93 95 96 98 99]
表2:
Product_ID | Product_Name | Product_Description
------------------------------------------------
1 | Coca-Cola | Coke description.
2 | Pepsi | Pepsi description.
3 | Fanta | Fanta description
...
我想查询数据库并将这两个表连接在一起,但是合并具有相同产品ID的重复行,以便返回的结果看起来像这样:
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
...
提前谢谢
答案 0 :(得分:1)
您可以对行进行分组,并使用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