【问题标题】:SQL query for getting records whose specific instance is not present in another table用于获取特定实例不存在于另一个表中的记录的 SQL 查询
【发布时间】:2021-03-03 15:28:58
【问题描述】:

我有 2 张桌子。我想从表 A 中获取所有从未上过大学的学生 id。 所以结果应该只返回“2”。

Table A
Student ID
1
2
3
Table B
1 - School
1 - College
2 - School
3 - School
3 - College

【问题讨论】:

标签: sql sql-server


【解决方案1】:

一种方法是使用not exists,如果您处理大量数据,这是最快的:

select *
from TableA
where not exists ( select 1 from table2 
                   where table1.studentid = tableb.studentid 
                   and schoolcol = 'college'
                 )

【讨论】:

    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2014-01-23
    • 1970-01-01
    相关资源
    最近更新 更多