Go back
Home
  • database
    • WCDB
      • mybatis
        • mybatis中的常见的运算符和表达式
      • mysql错误处理
        • Warning: mysqli_connect(): (HY000/2002)
      • postgresql
        • macos安装和配置
        • postgresql
        • join 'on' 与 'where'
        • postgresql导入导出(pg_dump)
        • 列名、关键字冲突的情况
        • 字符串按数字类型排序
        • postgresql安装(macos)
        • 逗号分割的字典值转字典名称(postgresql)
      • oracle
        • 时间处理
        • 锁表以及处理方法
      • sql
        • sql 查询left join项的个数
        • 求排名
        • 记录值是否存在另一个表中

记录值是否存在另一个表中

November 10, 2020
database

https://stackoverflow.com/questions/2686254/how-to-select-all-records-from-one-table-that-do-not-exist-in-another-table

最简单且性能最高的方案为:

SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE t2.name IS NULL

常规的方式为:

SELECT name
FROM table2
WHERE name NOT IN
    (SELECT name 
     FROM table1)