读书人

小弟我是SQL盲求一SQL语句

发布时间: 2012-05-10 16:02:39 作者: rapoo

我是SQL盲,求一SQL语句
表一: SBCRKBH

表二: SBKCJL

2表都有主键列 DH (对应关系是:SBCRKBH.DH=SBKCJL.DH)。

现在情况是,好多记录,DH 在 SBKCJL 中存在,在 SBCRKBH 没有了, 目的:

一、查询出 DH 在 SBKCJL 中存在,而 SBCRKBH 中没有的记录
二、删除 SBKCJL 的记录,条件是:DH 在 SBKCJL 中存在,而 SBCRKBH 中没有





[解决办法]

SQL code
1,select * from SBKCJL where DH not in(select DH from SBCRKBH)
[解决办法]
SQL code
2,delete from SBKCJL where DH not in(select DH from SBCRKBH)
[解决办法]
select *
from SBKCJL
where DH not in(select DH from SBCRKBH)


delete
from SBKCJL
where DH not in(select DH from SBCRKBH)

[解决办法]
select *
from SBKCJL
where DH not in(select DH from SBCRKBH)


delete
from SBKCJL
where DH not in(select DH from SBCRKBH)

[解决办法]
SQL code
select *  from SBKCJL  where DH not in(select DH from SBCRKBH)delete  from SBKCJL  where DH not in(select DH from SBCRKBH)
[解决办法]
一、查询出 DH 在 SBKCJL 中存在,而 SBCRKBH 中没有的记录

select t.* from SBKCJL t where not exists (select 1 from SBCRKBH n where n.dh = t.dh)
select t.* from SBKCJL t where dh not in (select dh from SBCRKBH)

二、删除 SBKCJL 的记录,条件是:DH 在 SBKCJL 中存在,而 SBCRKBH 中没有

delete SBKCJL from SBKCJL t where not exists (select 1 from SBCRKBH n where n.dh = t.dh)
delete SBKCJL from SBKCJL t where dh not in (select dh from SBCRKBH)

[解决办法]
1、select *
from SBKCJL
where DH not in(select DH from SBCRKBH)


2、delete
from SBKCJL
where DH not in(select DH from SBCRKBH)

[解决办法]
SQL code
--一、查询出 DH 在 SBKCJL 中存在,而 SBCRKBH 中没有的记录--二、删除 SBKCJL 的记录,条件是:DH 在 SBKCJL 中存在,而 SBCRKBH 中没有--1.SELECT * FROM SBKCJL AS aWHERE NOT EXISTS(SELECT 1 FROM SBCRKBH AS x WHERE x.DH=a.DH)--2.DELETE a FROM SBKCJL AS aWHERE NOT EXISTS(SELECT 1 FROM SBCRKBH AS x WHERE x.DH=a.DH) 

读书人网 >SQL Server

热点推荐