0

Dear Stackoverflowers,

I have a mysql query which checks if some sub categories are part of a categoryID to show all products from subcategories in the main category.

For example:

  1. Category
    • Subcategory 1
    • Subcategory 2
    • Subcategory 3

For example products are added to subcats but are ALL beeing shown in the head category. This works fine and i do this with WHERE cID in (' . $subids . ')

But now comes it, the cID used to hold just one value but since it has to be possible to add products to multiple categories i now save the multiple selected ids as comma separated in the cID field.

So im searching for a way to basicly find a match from a comma seperated list within a comma separated list because cID has now become comma separated ID's and FIND_IN_SET does not work with this.

I hope someone can shine some light, thank you all!

0

4 Answers 4

1

Have a read of these:

Implementing this, as opposed to comma-separated values, should probably save you and your coworkers a lot of headache and simplify your queries, effectively eliminating the need of complex queries. It should be faster, too.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to help a little more, if you implement the many to many data model your query would probably look something like this: SELECT * FROM table_a LEFT JOIN junction_table ON table_a.cID = junction_table.cID WHERE junction_table.subid IN ( $subids ) You may want to also add GROUP BY table_a.primary_key so you get a distinct record from table_a even if there are multiple matched subids.
0

Add full-text search index on this column in the database and then use following sql query Ref - http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

Comments

0

Storing a CSV in a single field is not a good idea. Searching on that requires use of LIKE clause.

where CID LIKE '$cid1,' or CID LIKE ',$cid1,' or CID LIKE ',$cid1' or 
      CID LIKE '$cid2,' or CID LIKE ',$cid2,' or CID LIKE ',$cid2' or
      .... so on

Also FIND_IN_SET would work too.

where FIND_IN_SET( $cid1, CID ) OR FIND_IN_SET($cid2, CID) .. so on

Comments

0

i now save the multiple selected ids as comma separated in the cID field. That's a very bad approach ,why would you store a CSV in a single field ,you shud change your design ,make a seperate table where each value will have a new row and will be mapped to a FK.

What you really need to do,is work on your DB design,read on Normalization and other related topics,I have so added some links,

  1. MSDN Link on Many to Many
  2. Databae Normalization

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.