0

I have the following tables

Product

id                      int(11)         AUTO_INCREMENT                                                  
name                    varchar(254)                                                                     
product_category_id     int(11)

A Product belongs to a category in ProductCategories. a category has sub category(self join)

id                      int(11)         AUTO_INCREMENT                                                  
name                    varchar(254)                                                                     
parent_id               int(11)

A Product also has a icon stored in ProductMedia.

id                      int(11)         AUTO_INCREMENT                                                  
url                     varchar(254)                                                  
type                    enum('icon','banner','video')                                                                     
product_id              int(11)

What is the most efficient way to get all products with it's associated icon that belong to a category including products in it's sub category if any.

Example:

Product  
1. iphone              - Mobile(category)
2. sIII                - Mobile (category) 
3. liginting connector - Cable(category)
4. iPhone USB charger  - Charger(category)

ProductCategories
1. Mobile   - 0(parent)
2. Cables   - 1(parent)
3. Sim      - 1
4. Shoes    - 0
5. Chargers - 2   

When i search for Mobile Category it needs to give me all 4 products, under cable it needs to give last 2 but under charger only last one

4
  • how is ProductMedia related to product? Jan 23, 2013 at 4:21
  • what do u mean by - "how do i also get products in the child categories there could be upto 2 levels of child categories" ?
    – DevT
    Jan 23, 2013 at 4:54
  • adding to @TilT's point, when you want products belonging to a specific category then the query becomes hierarchy agnostic.
    – uchamp
    Jan 23, 2013 at 6:02
  • Forget that I said that. Read the sentence once more and I understand what you're asking for. "get all products with icon for a given category (and all subcategories).
    – uchamp
    Jan 23, 2013 at 6:06

2 Answers 2

1

Use joins for this

SELECT 
    p.*,
    pc.name,
    pm.url,
    pm.type
FROM Product as p
LEFT JOIN ProductMedia as pm ON pm.product_id = p.id
LEFT JOIN ProductCategories as pc ON pc.id = p.product_category_id

You can do it like this
EDITS :

select
  p.name,
  ifnull(pc.Category,pc.name) as Category,
  pm.type
from product as p
  left join (select
           l.id        as id,
           l.name,
           l.parent_id,
           if(r.parent_id <> 0,l.name, CONCAT(l.name,'|',r.name )) as Category
         from productcategories as l
           left join productcategories as r
         on r.id = l.parent_id) as pc
    on pc.id = p.id
  left join productmedia as pm
    on pm.product_id = p.id

Output

name                        |   Category            |   type 
----------------------------------------------------------------
iphone                      |   Mobile              |   banner 
sIII                        |   Charger|Mobile      |   icon 
iphone liginting connector  |   HeadPhones|Mobile   |   video 

You can explode Category with php explode

2
  • how do i also get products in the child categories there could be upto 2 levels of child categories Jan 23, 2013 at 4:30
  • can you provide some samlpe data Jan 23, 2013 at 5:04
0

According to what i got from you question you want something like this.

SELECT 
    p.*,
    m.url,
    m.type
FROM Product as p
LEFT JOIN ProductMedia as m ON m.product_id = p.id
LEFT JOIN ProductCategories as c ON c.id = p.product_category_id
where c.id='Mobile'
2
  • i didn't get what you mean by -"and all subcategories"
    – DevT
    Jan 23, 2013 at 6:15
  • can't you do this with where clause? if not can u show sample data for ProductCategories table?
    – DevT
    Jan 23, 2013 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.