简体   繁体   中英

MySQL query with union returning wrong results

I'm having trouble figuring out the best way to do this MySQL statement.

The first table 'friends' has userID, friendID, and status

The second table 'users' has firstName, lastName, userID

The third table 'users_temp' has tempUserID, tempUserName

I'm looking to have one query pull all from 'friends' where userID or friendID = 1 and join the other field (if userID = 1 then grab friendID OR if friendID = 1 then grab userID) to 'users' / 'users_temp' where that userID = users.userID and left join users_temp if exists.

Right now I have this but it keeps returning the wrong results. Also the userID it returns has to be unique so I just return 1 per user (since this is a friends list)

SELECT users.userid, 
       users.fbid              AS fbID, 
       users.firstname         AS firstName, 
       users.lastname          AS lastName, 
       users.phonenumber       AS phoneNumber, 
       users.avatar            AS avatar, 
       users_temp.tempusername AS tempUserName, 
       status, 
       friendid 
FROM   users 
       JOIN (SELECT friendid, 
                    userid, 
                    status 
             FROM   friends 
             WHERE  friendid = $userid 
                    AND ( ( status != 2 ) 
                          AND ( status != 3 ) ) 
             UNION ALL 
             SELECT friendid, 
                    userid, 
                    status 
             FROM   friends 
             WHERE  userid = $userid 
                    AND ( ( status != 2 ) 
                          AND ( status != 3 ) )) foo 
         ON users.userid = foo.userid 
       LEFT JOIN users_temp 
              ON users.userid = users_temp.tempuserid 

I created a sqlfiddle which I hope will further explain my sql setup

http://sqlfiddle.com/#!2/690c3/4

Any help would be much appreciated!

Thanks,

Wes

As far as I can tell, the problem may be that you're doing a join with an incorrect field in the second part of your unionall query

Please try the following

SELECT users.userid, 
  users.fbid              AS fbID, 
  users.firstname         AS firstName, 
  users.lastname          AS lastName, 
  users.phonenumber       AS phoneNumber, 
  users.avatar            AS avatar, 
  users_temp.tempusername AS tempUserName, 
  status
FROM   users 
   JOIN (SELECT friendid AS joinid, 
            userid AS otherid, 
            status 
     FROM   friends 
     WHERE  friendid = 1 
            AND ( ( status != 2 ) 
                  AND ( status != 3 ) ) 
     UNION ALL 
     SELECT userid AS joinid, 
            friendid AS otherid, 
            status 
     FROM   friends 
     WHERE  userid = 1 
            AND ( ( status != 2 ) 
                  AND ( status != 3 ) )) foo 
   ON users.userid = foo.otherid 
  LEFT JOIN users_temp 
      ON users.userid = users_temp.tempuserid

Edit: Changed the column order on the second subquery of unionall. Do you really need the friendid column in the main query? or will otherid do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM