选择LEFT JOIN和WHERE CLAUSE

时间:2013-02-28 14:24:47

标签: mysql sql sql-server database

我有2个表:用户{用户名,用户ID}和奖品{用户ID,奖品,状态}

我想选择所有用户(来自用户左加入奖品),但在奖品表中有'status = dead'的用户除外

2 个答案:

答案 0 :(得分:4)

我认为您需要INNER JOIN,因为您只想搜索状态不等于dead的用户。

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

要进一步了解联接,请访问以下链接:

更新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'

答案 1 :(得分:-1)

试试这个;

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

由于