Question

I have a table named ad_categories and its structure is like this enter image description here

And i have another table named pub_categories and its structure is like this enter image description here

id in both table are different I need to select id's of ad_categories WHERE any of the ad_categories.cid should be in pub_categories.cid OR any of ad_categories.scid should be in ad_categories.scid OR any of the ad_categories.tcid should be in pub_categories.tcid i have cid list up to 35 ,scid up to 250 and tcid up to 600. How can achieve this any one please help me..

cid is main category scid is sub category and tcid is thrid subcategory. ad_category is where advertisers select categories and pub_catogries is publisher selecting categories. i have categories up to 30 subcatrgires up to 250 and thrid categories up to 600. both advertisers and publishers can select multiple categories i have tried by exploding pub_categories cid, scid and tcid by ',` and using like condition searching for the value like this

(ad_cat.cid LIKE '%,1,%' OR ad_cat.cid LIKE '%,1') OR (ad_cat.cid LIKE '%,2,%' OR ad_cat.cid LIKE '%,2')  OR (ad_cat.scid LIKE '%,1,%' OR ad_cat.scid LIKE '%,1') OR (ad_cat.scid LIKE '%,2,%' OR ad_cat.scid LIKE '%,2') OR (ad_cat.scid LIKE '%,3,%' OR ad_cat.scid LIKE '%,3') OR (ad_cat.scid LIKE '%,4,%' OR ad_cat.scid LIKE '%,4') OR (ad_cat.scid LIKE '%,5,%' OR ad_cat.scid LIKE '%,5')   OR (ad_cat.tcid LIKE '%,1,%' OR ad_cat.tcid LIKE '%,1') OR (ad_cat.tcid LIKE '%,2,%' OR ad_cat.tcid LIKE '%,2') OR (ad_cat.tcid LIKE '%,3,%' OR ad_cat.tcid LIKE '%,3') OR (ad_cat.tcid LIKE '%,4,%' OR ad_cat.tcid LIKE '%,4') OR (ad_cat.tcid LIKE '%,5,%' OR ad_cat.tcid LIKE '%,5')
Was it helpful?

Solution

For matching comma separated with comma separated columns, you have only one choice to have a RegExp in your query. Rule is like:

ad_categories.cid REGEXP '(^|,)(11|2|4)(,|$)'

or

ad_categories.cid REGEXP '(^|,)(REPLACE(pub_categories.cid, ',', '|'))(,|$)'

Hope this will help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top