I tried searching for an answer, but didn't find anything, and I'm not sure if I'm describing this in the best way, but here goes...
So, I have a MySQL database, and a website that the user can select 0-3 categories to search the database on.
For example, if I have records like this in the DB:
id|cat1|cat2|cat3
-----------------
1 | a | b | c
2 | a | |
3 | z | b | a
4 | y | a |
5 | b | c | a
I'm trying to figure out a way so if the user selects, for example, 1 category to search on and it's "a", then it would return records [1,2,3,4,5], and if the user selects 2 categories to search on, like "a" and "b" it would return records [1,3,5], and it the user selected 3 categories, like "a" and "c" and "b", then it would return [1,5].
There is no guarantee on sequence, neither in the DB nor in what the user enters.
I've gotten my query to where it can select records that have any of the entered categories, but I can't seem to figure out how to get to return only records that have ALL the categories provided.
Does anyone know how to do that?
Thanks!
Here's what I have so far, it's messy and I've just been kind of shooting in the dark here, haha, but here it is (in this example, the categories "Church" and "Occupation" were selected):
SELECT * FROM all_terms
WHERE
( (subject1 in ('Church', 'Occupation') OR subject1 IS NULL OR subject1 = '')
AND (subject2 in ('Church', 'Occupation') OR subject2 IS NULL OR subject2 = '')
AND (subject3 in ('Church', 'Occupation') OR subject3 IS NULL OR subject3 = '')
AND ( (subject1 IS NOT NULL && subject1 != '')
OR (subject2 IS NOT NULL && subject2 != '')
OR (subject3 IS NOT NULL && subject3 != '')))