简体   繁体   中英

SELECT WITH LEFT JOIN AND WHERE CLAUSE

I have 2 tables: Users{username, UserID} AND Prizes{UserID, prize, status}

I want to select all the users (from Users Left Join Prizes) except the users who has 'status = dead' in the Prizes table

I think you need here is INNER JOIN because you only want to search fors user having status not equal to dead .

SELECT  a.*, b.*
FROM    Users a
        INNER JOIN Prizes b
            ON a.userID = b.UserID
WHERE   b.status <> 'dead'

To further gain more knowledge about joins, kindly visit the link below:

UPDATE 1

SELECT  a.*, b.*
FROM    Users a
        LEFT JOIN Prizes b
            ON a.userID = b.UserID
WHERE   b.UserID IS NULL OR b.status <> 'dead'

Try this;

select u.* 
from users as u 
left join prizes as p 
on u.userid = p.userid 
where p.status <>'dead';

Thanks

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