开发者

MySQL conditional statement on NULL with subquery

开发者 https://www.devze.com 2023-02-14 03:55 出处:网络
Two tables: user (id, myValue, ...) user_preferred (id, userid, preferredValue) fk userid -> user(id)

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 开发者_如何学Pythonpreferred 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?


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

IF (Result IS NULL,1,RESULT)


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
         )
       )
0

精彩评论

暂无评论...
验证码 换一张
取 消