【问题标题】:How do I get the records from the left side of a join that do not exist in the joined table?如何从联接左侧获取联接表中不存在的记录?
【发布时间】:2012-06-29 13:24:06
【问题描述】:

我有表a加入表b,我只想要表a中不存在在表b中的记录。

我的部分查询是:

SELECT DISTINCT u.id, u.lastname
FROM user u
LEFT JOIN context ct
ON ct.userid = u.id

谢谢。

【问题讨论】:

    标签: sql left-join


    【解决方案1】:
    select distinct u.id,
        u.lastname
    from user u
    left join context ct on ct.userid = u.id
    where ct.userid is null
    

    【讨论】:

    • 唯一需要注意的是,您需要使用不采用 NULL 值的列。如果没有,可以使用子查询,例如 (select ct.*, 1 as col from context ct) ct 来获取合适的列。
    猜你喜欢
    • 2021-02-25
    • 2020-04-09
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多