代码表:tbAddress:xh(integer),country(char),town(char),village(char),主键为country+ town+ village
xh country town village
1 苏仙区 良田镇 良田村
2 苏仙区 良田镇 堆上村
3 苏仙区 坳上镇 坳上村
4 苏仙区 坳上镇 桥头村
。。
数据表:tbobject:sfid(char),csny(date),xb(char),country(char),town(char),village(char)
查找tbobject中country、town、village不是tbaddress中的值的记录
xh country town village
1 苏仙区 良田镇 良田村
2 苏仙区 良田镇 堆上村
3 苏仙区 坳上镇 坳上村
4 苏仙区 坳上镇 桥头村
。。
数据表:tbobject:sfid(char),csny(date),xb(char),country(char),town(char),village(char)
查找tbobject中country、town、village不是tbaddress中的值的记录
解决方案 »
- [问个最菜的问题]如何用SQL语句得到当前正在使用的数据库的名称
- 我的update方法,不知会不会有后遗症或别的什么
- $$安装sql2000时遇到的问题,头大了,请帮忙吧,我是初学者,感谢!
- 在sqlserver中怎么将一个时间字符串值转换成时间戳的数字形式呢
- 唉,怎么什么都忘了,超简单问题,送分喽...
- 求高手做一个复杂查询
- fastreport 打印
- 如何实现Dbase表导入SQL server将char(10)字段转换为smallint
- 请教高手:如何将连本机数据库的c#程序移植到别的机器运行
- 设计sql表时,小数点后面的位数怎么定???
- 简单SQL 表格倒腾的问题
- SQL中如何设置超时的时间???
select *
from tbobject
where (country + town + village) not in
(select country + town + village from tbAddress)
not exists (select 1 from tbAddress where country+town+village=a.country+a.town+a.village)
select * from tbobject a where not exists(select 1 from tbAddress where a.country=country and a.town=town and a.village=village)
WHERE (country,town,village) NOT IN (SELECT country,town,village FROM tbAddress)
from tbobject o
left outer join tbaddress s
on o.country = s.country
and o.town = s.town
and o.village = o.village
where s.xh is null
/*
xh country town village
1 苏仙区 良田镇 良田村
2 苏仙区 良田镇 堆上村
3 苏仙区 坳上镇 坳上村
4 苏仙区 坳上镇 桥头村
*/-->测试数据
create table tbAddress(xh int,country varchar(20),town varchar(20),village varchar(20))
insert into tbAddress
select 1,'苏仙区','良田镇','良田村' union all
select 2,'苏仙区','良田镇','堆上村' union all
select 3,'苏仙区','坳上镇','坳上村' union all
select 4,'苏仙区','坳上镇','桥头村'
gocreate table tbObject(sfid varchar(10),csny datetime,xb varchar(10),country varchar(20),town varchar(20),village varchar(20))
insert into tbObject
select 1,'2011-1-1','a','苏仙区','良田镇','良田村' union all
select 2,'2011-1-1','b','苏仙区','良田镇','堆上村' union all
select 3,'2011-1-1','a','苏仙区','坳上镇','坳上村' union all
select 4,'2011-1-1','c','苏仙区','坳上镇','桥头村' union all
select 5,'2011-1-1','b','苏仙区','良田镇','坳上村' union all
select 6,'2011-1-1','d','苏仙区','坳上镇','良田村' union all
select 7,'2011-1-1','d','坳上镇','苏仙区','苏仙区' union all
select 8,'2011-1-1','e','桥头村','坳上镇','苏仙区'
go-- 1
select *
from tbObject
where (country + town + village) not in
(select country + town + village from tbAddress)--2
select *
from tbObject t
where not exists (select 1 from tbAddress e where e.country = t.country and e.town = t.town and e.village = t.village)drop table tbAddress,tbObjectsfid csny xb country town village
---------- ----------------------- ---------- -------------------- -------------------- --------------------
5 2011-01-01 00:00:00.000 b 苏仙区 良田镇 坳上村
6 2011-01-01 00:00:00.000 d 苏仙区 坳上镇 良田村
7 2011-01-01 00:00:00.000 d 坳上镇 苏仙区 苏仙区
8 2011-01-01 00:00:00.000 e 桥头村 坳上镇 苏仙区(4 行受影响)
--3个不同时相等
SELECT * FROM tbobject
WHERE country+town+village NOT IN
(SELECT country+town+village FROM tbAddress)--任何一个都不等
SELECT * FROM tbobject
WHERE country NOT IN (SELECT DISTINCT country FROM tbAddress)
AND town NOT IN(SELECT DISTINCT town FROM tbAddress)
AND village NOT IN(SELECT DISTINCT village FROM tbAddress)