sql server 疑难问题 delete from aa where id not in (select max(id) from bb group by name) 不能运行
而
delete from aa where id in (select max(id) from bb group by name) 能运行
请问什么原因啊
------解决方案-------------------- 啥症狀?
------解决方案-------------------- 开始提示如下: 警告: 聚合或其它 SET 操作消除了空值。
(所影响的行数为 0 行) 后来我设置了: set ansi_warnings off 提示: (所影响的行数为 0 行)
------解决方案-------------------- 不知道,楼主的数据怎么样,我的测试了一下没有问题 --> 测试数据: # if object_id('tempdb.dbo.#') is not null drop table # go create table # (id int,name varchar(4),typename varchar(4),id1 int) insert into # select 1,'小张','青春',1 union all select 2,'小黄','成熟',2 union all select 2,'小黄','贤惠',3 union all select 2,'小黄','清纯',4 union all select 3,'小美','清纯',5 union all select 3,'小美','成熟',6 union all select 4,'小林','美丽',7 union all select 4,'小林','聪明',8 union all select 4,'小林','大方',9
go delete from # where id1 not in(select max(id1) from # group by id) go select * from #
delete from aa where id not in (select max(id) from bb group by name) 不能运行 : 当你的id不在这个结果集(select max(id) from bb group by name)里,既id不存在,那你怎么删除呢?删除空行,是不能运行成功的。
delete from aa where id in (select max(id) from bb group by name) 能运行: 当id在这个结果集(select max(id) from bb group by name)里,既id存在,所以你删除时能正常执行。
------解决方案-------------------- select max(id) from bb group by name