简体   繁体   中英

Best way to query multiple 'categories' mysql php?

I am probably overlooking something simple--

Some of the news items has 2 categories checked, and other times it has all 4 check. I know I can do OR '' OR '' OR '' OR '' -- but I think there is a better way.

The question: How could I query this to output: if the story has category 10,13 or 10,13,15 or 10,13,15,16 or 10,15 or 10,13?? ('10' is the parent category) -- without doing a query like that??

WHERE news.id = story.post_id AND news.category LIKE '10,13,15,16'

You could say:

AND new.category IN ('10,13', '10,13,15', '10,13,15,16', '10,15', '10,13')

Which is slightly cleaner that using many OR 's.

WHERE news.id = story.post_id AND news.category in (10, 13, 15, 16)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM