Selectively applying user ID in WHERE clause to entries

Hi,

I been going back and forth with IF and CASE and a whole lot of other suggestions found elsewhere trying to figure out how to filter out certain entries based on category and user ID. Assume that a table user_posts contains posts made by a lot of users. I want to get results restricted to the user_ID=‘MIRA76’ for posts that are in categories ‘CAT_64’ or ‘CAT_106’. However, I still want to get results by other users from other categories.

TABLE user_posts

[table=“width: 500, class: grid, align: left”]
[tr]
[td]post_ID[/td]
[td]category_ID[/td]
[td]user_ID[/td]
[td]content[/td]
[/tr]
[tr]
[td]456FQ[/td]
[td]CAT_64[/td]
[td]MEK90[/td]
[td]TEXTUAL CONTENT[/td]
[/tr]
[tr]
[td]754GD[/td]
[td]CAT_21[/td]
[td]MEK90[/td]
[td]TEXTUAL CONTENT[/td]
[/tr]
[tr]
[td]616CZ[/td]
[td]CAT_106[/td]
[td]MIRA76[/td]
[td]TEXTUAL CONTENT[/td]
[/tr]
[tr]
[td]714PA[/td]
[td]CAT_64[/td]
[td]MIRA76[/td]
[td]TEXTUAL CONTENT[/td]
[/tr]
[tr]
[td]554WU[/td]
[td]CAT_26[/td]
[td]RON32[/td]
[td]TEXTUAL CONTENT[/td]
[/tr]
[tr]
[td]451YD[/td]
[td]CAT_41[/td]
[td]RON32[/td]
[td]TEXTUAL CONTENT[/td]
[/tr]
[tr]
[td]783TD[/td]
[td]CAT_21[/td]
[td]MIRA76[/td]
[td]TEXTUAL CONTENT[/td]
[/tr]
[tr]
[td]156GL[/td]
[td]CAT_11[/td]
[td]MEK90[/td]
[td]TEXTUAL CONTENT[/td]
[/tr]
[tr]
[td]274HD[/td]
[td]CAT_11[/td]
[td]HOM92[/td]
[td]TEXTUAL CONTENT[/td]
[/tr]
[/table]

In the example entries, for categories ‘CAT_64’ and ‘CAT_106’ only those by the user_ID ‘MIRA76’ is taken, but for other categories there is no such restriction. How do I go about doing this? Here those in green highlight are the ideal results.

WHERE category_id IN ('CAT_64','CAT_106') AND user_id = 'MIRA76' 
   OR category_id NOT IN ('CAT_64','CAT_106')

This seems to do exactly what I want, my actual table contains 10k+ entities so it’s difficult to go over it to make sure. So just to confirm, it will also fetch posts by user_ID MIRA76 in the category CAT_21, yes?

yes, posts by all users in that category

Great! Thank you very much :smiley: