Two tables:
user (id, myValue, ...)
user_preferred (id, userid, preferredValue) fk userid -> user(id)
Explanation:
user is a table of all users. user_preferred is a table of any user’s preferred values. A user can be listed multiple times in user_preferred but must have different values. Query should return all users that have a myValue that matches the preferred value of the given user. $userid is the php variable of the user passed.
The Trick:
A user could have no preference, in which case there is no entry in the user_preference table. When the above query is done, I want to return every user if the given user has no preference.
Analogy:
I’m at a bar and the bartender asks me what I want to drink. I say give me everything he has that matches my preference. The first round I say I like crappy beers. So he gives me a Fosters. Second round I say I have no preference and he gives me 12 pints ranging from Bud Light to Guinness. Instead of beers, these would be users. Get it?
Query (so far):
SELECT * FROM user WHERE
IF ((SELECT preferredValue FROM user_preferred WHERE userid = $userid) IS NULL,
1,
user.myValue ANY (SELECT preferredValue FROM user_preferred WHERE userid = $userid)
)
Additional Trick:
I don’t want to run “SELECT preferredValue FROM user.preferred where id = $userid” twice. Can I save the results from the first run-time and somehow use it in place of the second?
To return a list of users: