Reputation: 302
I have four tables I want to join together, but one table I want to group_concat two specific records.
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)
What I want to do is below:
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
Basically, I'm trying to load the Product with the Category and Attribute. It is working fine by just left joining the tables without the Product_attribute PA. I can't figure out how to put the attribute together. Below is my code:
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)
I apologize if this is a bit confusing
Updated 04/26/2013
Here a summary what goes through in the tables and what I want to accomplish: Product_to_category PC table links a product to their categories and a category to their products. A product could belong to multiple categories and a category could belong to multiple product. A product in P table has their product values like price, and its description is in product_description PD table. A product could have two rows of product description (bilingual). A product has attribute values as well in product_attribute PA table like brand ('Brand A', 'Brand B') and volume/weight ('300g', '500ml'). A product could belong to multiple categories, and a category could have multiple products. Each category has two descriptions (bilingual). What I want to accomplish to show each product with their multiple categories in the correct language, and concatenate the attribute text.
Please let me know if there needs further explanation.
Upvotes: 1
Views: 137
Reputation: 781004
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
Upvotes: 2