相关疑难解决方法(0)

EXISTS (SELECT 1 ...) vs EXISTS (SELECT * ...) 一个还是另一个?

每当我需要检查表中某行是否存在时,我总是倾向于编写如下条件:

SELECT a, b, c
  FROM a_table
 WHERE EXISTS
       (SELECT *  -- This is what I normally write
          FROM another_table
         WHERE another_table.b = a_table.b
       )
Run Code Online (Sandbox Code Playgroud)

还有一些人这样写:

SELECT a, b, c
  FROM a_table
 WHERE EXISTS
       (SELECT 1   --- This nice '1' is what I have seen other people use
          FROM another_table
         WHERE another_table.b = a_table.b
       )
Run Code Online (Sandbox Code Playgroud)

当条件NOT EXISTS不是EXISTS: 在某些情况下,我可能会用 aLEFT JOIN和一个额外的条件(有时称为antijoin)来编写它:

SELECT a, b, c
  FROM a_table
       LEFT JOIN another_table ON another_table.b = …
Run Code Online (Sandbox Code Playgroud)

mysql postgresql oracle sql-server

47
推荐指数
5
解决办法
11万
查看次数

标签 统计

mysql ×1

oracle ×1

postgresql ×1

sql-server ×1