日期:2014-05-18  浏览次数:20513 次

sql 语句求解
表数据如下:
-----------------
A a1 ...其他字段
A a2
B b1
C c1
------------------
现在想根据前两个字段删除数据,
例如有数据:
A a1
C c1
我想把前两个字段不是这个的都删了(即删掉A a2和B b1),sql语句怎么写

------解决方案--------------------
你例如中的数据是哪里来的?临时表还是其他有的表?

SQL code

--记得用测试库哈~!
delete t
from 原表 t left join 参照数据表 e on t.字段1 = e.字段1 and t.字段2 = e.字段2
where e.字段1 is null

------解决方案--------------------
DELETE T FROM T WHERE NOT EXISTS(SELECT 1 FROM TC WHERE COL1=T.COL1 AND COL2=T.COL2)

------解决方案--------------------
SQL code
CREATE TABLE TA (COL1 VARCHAR(12),COL2 VARCHAR(12))
INSERT TA SELECT 'A','a1' union select  'A','a2'union select  'B','b1' union select  'C','c1'



delete from ta  where not exists (select 1 from (

select * from TA  where (col1='A' AND col2='a1') or (col1='C' AND col2='c1'))a where col1=ta.col1 and a.col2=ta.col2)
/*
COL1    COL2
A    a1
C    c1
*/

------解决方案--------------------
SQL code



delete  from  tab 
where  not  exists ( select *  from tab  where  A='a1' and C='c1')