在MySQL中加入多个表并合并数据

时间:2013-04-25 18:17:18

标签: mysql join concat

我有四个表要加入,但是我想要一个表group_concat两个特定的记录。

Product_to_category PC
P_ID C_ID
1     1
2     2
2     3
1     3


Product P
P_ID P_PRICE
1    12
2    3
3    4

Product_Description PD
P_ID  Language_id   PD_Name
1       1           Chips
1       2           Chips (In a different language)
2       1           Soda
2       2           Soda (In a different language)

Category Description CD
C_ID   Language_id  CD_Name
1      1            Junk Food
1      2            Junk Food (In a different language)
2      1            Drinks
2      2            Drinks (In a different language)
3      1            Imported
3      2            Imported (In a different language)


Product_attribute PA
P_ID  A_ID  Language_ID  A_TEXT
1     1      1            Brand A
1     2      1            300g
1     3      1            Ketchup
1     1      2            Brand A
1     2      2            300g 
1     3      2            Ketchup (Different language)
2     1      1            Brand B
2     2      1            500mL
2     3      1            Cherry
2     1      2            Brand B
2     2      2            500mL
2     3      2            Cherry (Different language)

我想做的是:

P.P_ID |  P.P_PRICE | PD.LANGUAGE_ID | PD.PD_NAME | CD.C_ID | CD.CD_NAME | Attribute
------------------------------------------------------------------------------------
  1    |  12        |   1            | Chips      |  1      | Junk Food | Brand A 300g
  1    |  12        |   2            | Chips      |  1      | Junk Food | Brand A 300g
  2    |  3         |   1            | Soda       |  2      | Drinks    | Brand B 500mL
  2    |  3         |   2            | Soda       |  2      | Drinks    | Brand B 500mL
  2    |  3         |   1            | Soda       |  3      | Imported  | Brand B 500mL
  2    |  3         |   2            | Soda       |  3      | Imported  | Brand B 500mL
  1    |  12        |   1            | Chips      |  3      | Imported  | Brand A 300g
  1    |  12        |   2            | Chips      |  3      | Imported  | Brand A 300g

基本上,我正在尝试使用类别和属性加载产品。只需在不使用Product_attribute PA的情况下连接表格,它工作正常。我无法弄清楚如何将属性放在一起。以下是我的代码:

select P.P_ID, P.P_PRICE, PD.LANGUAGE_ID, PD.PD_Name, CD.C_ID, CD.CD_NAME, 
(select group_concat(PA.A_TEXT) from PA where (PA.A_ID=1 or PA.A_ID=2) and PA.P_ID =
P.P_ID group by PA.P_ID) as attribute from PC left join PD on PC.P_ID = PD.P_ID
left join CD on PC.C_ID = CD.C_ID
left join P on P.P_ID = PC.P_ID
where PD.language_id = CD.language_id and (PD.language_id=1 or PD.language_id=2)

如果这有点令人困惑,我道歉

更新于04/26/2013
这里总结了表中的内容和我想要完成的内容:Product_to_category PC表将产品链接到他们的类别和产品的类别。产品可以属于多个类别,类别可以属于多个产品。 P表中的产品的产品价值如价格,其描述在product_description PD表中。产品可以有两行产品描述(双语)。产品在product_attribute PA表中也具有属性值,如品牌('品牌A','品牌B')和体积/重量('300g','500ml')。产品可以属于多个类别,而类别可以包含多个产品。每个类别都有两种描述(双语)。我想要用正确的语言显示每个产品的多个类别,并连接属性文本。

如果需要进一步解释,请告诉我。

1 个答案:

答案 0 :(得分:2)

select P.P_ID, P.P_PRICE, PD.LANGUAGE_ID, PD.PD_Name, CD.C_ID, CD.CD_NAME, 
group_concat(DISTINCT PA.A_TEXT) as attribute
from PC left join PD on PC.P_ID = PD.P_ID
left join CD on PC.C_ID = CD.C_ID
left join P on P.P_ID = PC.P_ID
left join PA on PA.P_ID = P.P_ID AND (PA.A_ID=1 or PA.A_ID=2)
where PD.language_id = CD.language_id and (PD.language_id=1 or PD.language_id=2)
GROUP BY P.P_ID, PD.LANGUAGE_ID

FIDDLE