sql查询选择不在另一个表中的值 [英] sql query select values not in another table

查看:99
本文介绍了sql查询选择不在另一个表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个select查询,它比较两个不同的本地数据库的两个表,并返回第二个表中不存在的一个表的值,试过这个但是有错误

< pre lang =SQL> 声明 library.dbo.phones as t1

选择 * 来自 [library] .dbo.table_1 as t2 其中 t2.onoma 喜欢 t1.name



或者这个

 声明 library.dbo.phones  as  t1 

select * < span class =code-keyword>来自 [library] .dbo.table_1 as t2 其中 t2.onoma t1.name

解决方案

使用不存在 [ ^ ]

一个例子:

 选择 * 来自 [databasename] .dbo.table1 t1 其中 
存在

[databasename] .dbo.table2 t2 其中 t1.name = t2.onoma


是的,你可以使用不在



 选择 * 来自 [databasename] .dbo.table1  where     in 选择  来自 [databasename] .dbo.table2)


I want to have a select query which compares 2 tables of different both local databases and return the values of one table where they are not exists in second table, tried this but there are errors

declare library.dbo.phones as t1
 
 select * from [library].dbo.table_1 as t2 where t2.onoma Not like  t1.name


or this

declare library.dbo.phones as t1
 
 select * from [library].dbo.table_1 as t2 where t2.onoma Not in  t1.name

解决方案

Use Not Exists[^]
An example:

select * from [databasename].dbo.table1 t1 where
not exists
(
select * from [databasename].dbo.table2 t2 where t1.name = t2.onoma
)


Yeah, you can use Not in

select * From [databasename].dbo.table1 where Column not in(select column from [databasename].dbo.table2 )


这篇关于sql查询选择不在另一个表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆