我想只删除以下语句返回的行,我该怎么做?
select usersernum,username,useremail
from core.appuser
where usersernum in (
select ch.usersernum
from core.appusersession ch
where ch.usersernum not in (
select usersernum
from (
select max(starttime) as maxstarttime
, usersernum
from core.appusersession vip
where vip.starttime >= '2011-01-01 00:00:00'
group by usersernum
)
)
group by usersernum
)
order by useremail
;
答案 0 :(得分:1)
您可以使用
DELETE FROM core.appuser WHERE usersnum IN (
select usersernum
from core.appuser
where usersernum in (
select ch.usersernum
from core.appusersession ch
where ch.usersernum not in (
select usersernum from (
select max(starttime) as maxstarttime, usersernum
from core.appusersession vip where vip.starttime>='2011-01-01 00:00:00' group by usersernum))
group by usersernum)
order by useremail);
答案 1 :(得分:0)
您没有说明您正在运行的DB2平台或版本,但如果您使用的是DB2 9.7 for Linux,UNIX和Windows或更高版本,则可以使用超级便捷的“从选择中删除”:
delete from (
select usersernum,username,useremail
from core.appuser
where usersernum in (
select ch.usersernum
from core.appusersession ch
where ch.usersernum not in (
select usersernum
from (
select max(starttime) as maxstarttime
, usersernum
from core.appusersession vip
where vip.starttime >= '2011-01-01 00:00:00'
group by usersernum
)
)
group by usersernum
)
order by useremail
);
答案 2 :(得分:0)
有时退后一步并问自己,这里的真正目标是什么,“业务术语”或日常语言而不是SQL。
您的查询看起来有一堆不必要的图层。您真正想要做的就是删除所有非活动用户,即自2011年初以来尚未开始会话的用户。
DELETE FROM core.appuser
WHERE usersernum NOT IN
( SELECT DISTINCT usersernum
FROM core.appusersession
WHERE starttime>='2011-01-01 00:00:00'
);
不是那么简单吗?