Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 联接并选择表2中未包含的值_Sql - Fatal编程技术网

Sql 联接并选择表2中未包含的值

Sql 联接并选择表2中未包含的值,sql,Sql,我很感激这对你们来说可能很简单,但有时候JOIN背后的逻辑对初学者来说可能很难。我想从表1中选择“ID”,但只选择那些没有出现在表2中的“ID”。我测试了左右,但无法让它按我需要的方式工作。我正在使用dashDB。您可以在和子查询中使用NOT Select * from table1 where id NOT IN (select id from table2); 试试这个 SELECT * FROM table1 LEFT JOIN table2 ON table1.ID = table2.

我很感激这对你们来说可能很简单,但有时候JOIN背后的逻辑对初学者来说可能很难。我想从表1中选择“ID”,但只选择那些没有出现在表2中的“ID”。我测试了左右,但无法让它按我需要的方式工作。我正在使用dashDB。

您可以在和子查询中使用NOT

Select * from table1 where id NOT IN (select id from table2);
试试这个

SELECT *
FROM table1
LEFT JOIN table2 ON table1.ID = table2.ID
WHERE table2.ID IS NULL
使用下面的脚本

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

我总是希望
不存在
这样做

Select * from table1 a  
where NOT EXISTS (select 1 from table2 b where a.id = b.id);
下面是Aaron Bertrand的一篇优秀文章,它比较了所有方法的性能


向我们展示您的左连接尝试。您也应该发布您的查询..喂,我不熟悉dashDB,但请看一看,如果您提供您尝试过的代码,所有人都会更容易理解您的问题,或者您想要获得的演示/详细信息/输出
从table1左键选择table1.ID在table1.ID=table2.ID中连接table2.ID,其中table2.ID为null
尝试使用此查询。你在开玩笑吗?这么简单?我觉得自己像个傻瓜。谢谢@KugutsumenNote,如果table2的id为NULL,则不会返回任何行。@jarlh谢谢,这是一个很好的观点。幸运的是,在这种特殊情况下这是不可能的,但还是要感谢你you@MikePala-检查更新,这是一篇有用的文章。但它特定于
SQL SERVER