开发者

Database design for product catalog site

I am working on an e-commerce store consisting of products and categories. I have 3 main ways of filtering products: by category, by manufacturer and by gender.

I have the following tables

products
- id
- name
- manufacturer_id
- gender_id

categories
- id
- name
- parent_id

products2categories
- id
- product_id
- category_id

manufacturers
- id
- name

gender
- id
- name

Using these tables I can list products e.g. find all products where manufacturer_id = 1, gender = 1, category= 453.

However I want to have the control of setting images, page titles, meta tags, display order etc for each of these filter combinations. So I have added another table:

pages
- id
- category_id
- manufacturer_id
- gender_id
- image
- page_title
- meta_description
- display_order

This works, but I now have the task of making sure every single possible combination is in this table. For example when adding a new product, I will need to create all the combinations. Also if I want to add more filters in the future, t开发者_如何学编程he table is going to get bigger and bigger. Is there another approach I could use, or is this the only way?


You could use "default" or "fallback" records which would be selected if there is no exact match, like this:

SELECT  p.*, g.*
FROM    products p
JOIN    product2categories pс
ON      pc.product_id = p.id
LEFT JOIN
        pages gcgm
ON      gcgm.category_id = pc.category_id
        AND gcgm.gender_id = p.gender_id
        AND gcgm.manufacturer_id = p.manufacturer_id
LEFT JOIN
        pages gcg
ON      gcg.category_id = pc.category_id
        AND gcg.gender_id = p.gender_id
        AND gcg.manufacturer_id IS NULL
        AND gcgm.id IS NULL
LEFT JOIN
        pages gc
ON      gc.category_id = pc.category_id
        AND gc.gender_id IS NULL
        AND gc.manufacturer_id IS NULL
        AND gcg.id IS NULL
JOIN    pages g
ON      g.id = COLAESCE(gcgm.id, gcg.id, gc.id)
WHERE   pc.category_id = 453
        AND p.manufacturer_id = 1
        AND p.gender = 1

This will first try to select the complete combination of (gender, manufacturer, collection) and will fall back first to (collection, gender) then to (collection) on failure to do so.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜