BlackHat
BlackHat

Reputation: 79

MySQL conditional statement on NULL with subquery

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?

Upvotes: 1

Views: 1452

Answers (2)

spencer7593
spencer7593

Reputation: 108480

To return a list of users:

SELECT o.*
  FROM `user` o
 WHERE o.id IN
     ( SELECT DISTINCT m.userid
         FROM user_preferred m
        WHERE EXISTS 
              ( SELECT 1 FROM user_preferred p
                 WHERE p.preferredValue = m.preferredValue 
                   AND p.userid <> m.userid 
                   AND p.userid = $userid )
     )
    OR ( o.id <> $userid AND NOT EXISTS
         ( SELECT 1
             FROM user_preferred q
            WHERE q.userid = $userid
         )
       )

Upvotes: 0

AbiusX
AbiusX

Reputation: 2404

SELECT *,(SELECT preferredValue FROM user_preferred WHERE userid = $userid) AS Result FROM user WHERE

IF (Result IS NULL,1,RESULT)

Upvotes: 2

Related Questions