php - searching strings in SQL -


i'm building simple shopping cart. need allow related products. initial thought have db field in product table called tags have comma delimited list of tags in it:

tag1,tag2,tag3 

when grab product db grab tags , explode string on comma.

problem i'm having trouble thinking of way call db other products have matching tag. there way search string in sql?

can think of way achieve this

i not go down route of storing comma separated strings in fields, not scalable (or normalized) . split 3 different tables:

 products: ------------- id | name -------------  1  | product 1 2  | product 2   tags: --------------- id | tag ---------------  1  | tag 1 2  | tag 2   product_tags: ---------------------- product_id | tag_id ---------------------- 1          | 1 1          | 2 

when want find products related tags do

      select product_id product_tags tag_id = tag_id  

you use more advanced joining statements return records products table (instead of product_id's tags table):

      select products.* products      inner join product_tags on product_tags.product_id = products.id     product_tags.tag_id = tag_id  

its bit more work save headaches in future.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -