where 带条件的判断

lily_ok 2007-03-21 01:50:11
id mark age
1 0 90
1 0 80
1 1 6
1 1 2
1 2 28
1 2 50

我想实现的是
当mark=0的时候,删除age>3的计录
当mark=1的时候,删除age>30的计录
当mark=2的时候,删除age>80的计录
执行完SQL语句后
数据库应该是
id mark age
1 0 80
1 1 2
1 2 28

我自己写的是
delete table where
case
WHEN mark ='0' THEN age>'3'
WHEN mark ='2' THEN age>'30'
else THEN age>'90'
end
可以不行,谁能帮 我学一个带条件判断的类似的语句啊

...全文
282 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zsl5305256 2007-03-21
  • 打赏
  • 举报
回复
declare @mark int
select @mark=case when 0 then 'age>3'
case when 2 then 'age>30'
else 'age>90'
end
exec('delete table where'+@mark)
kadboy 2007-03-21
  • 打赏
  • 举报
回复
delete from table
where age>case when mark=0 then 80 when mark=1 then 3 when mark=2 then 30 else age end
mark age case...end 后的值 是否删除

0 79 80 否
0 80 80 否
0 81 80 是
1 2 3 否
1 3 3 否
1 4 3 是
2 29 30 否
2 30 30 否
2 31 30 是
kadboy 2007-03-21
  • 打赏
  • 举报
回复
delete from table
where age>case when mark=0 then 3 when mark=1 then 30 when mark=2 then 80 else age end
mark age case...end 后的值 是否删除

0 2 3 否
0 3 3 否
0 4 3 是
1 29 30 否
1 30 30 否
1 31 30 是
2 79 80 否
2 80 80 否
2 81 80 是
kadboy 2007-03-21
  • 打赏
  • 举报
回复


delete from table
where age>case when mark=0 then 80 when mark=1 then 3 when mark=2 then 30 else age end
OracleRoob 2007-03-21
  • 打赏
  • 举报
回复
用 OR 连接多个完整条件即可。
lily_ok 2007-03-21
  • 打赏
  • 举报
回复
能不能有WHEN THEN 来做啊,我主要是想知道,这样的条件判断该如何写
OracleRoob 2007-03-21
  • 打赏
  • 举报
回复

delete 表名
where (mark ='0' and age>'3') or (mark ='1' and age>'30') or (mark ='2' and age>'80')



--如果是数值型
delete 表名
where (mark =0 and age>3) or (mark =1 and age>30) or (mark =2 and age>80)
lily_ok 2007-03-21
  • 打赏
  • 举报
回复
对还有,和id 那列没有关系,不好意思,发的有点急
OracleRoob 2007-03-21
  • 打赏
  • 举报
回复
delete 表名
where (mark ='0' and age>'3') or (mark ='2' and age>'30') or (age>'90')



--如果是数值型
delete 表名
where (mark =0 and age>3) or (mark =2 and age>30) or (age>90)


lily_ok 2007-03-21
  • 打赏
  • 举报
回复
不好意思,贴子的数据有点问题,这下面的数据是对地
不好意思,贴子的数据有点问题,这下面的数据是对地
不好意思,贴子的数据有点问题,这下面的数据是对地

id mark age
1 0 90
1 0 80
1 1 6
1 1 2
1 2 28
1 2 50

我想实现的是
当mark=0的时候,删除age>80的计录
当mark=1的时候,删除age>3的计录
当mark=2的时候,删除age>30的计录
执行完SQL语句后
数据库应该是
id mark age
1 0 80
1 1 2
1 2 28

我自己写的是
delete table where
case
WHEN mark ='0' THEN age>'80'
WHEN mark ='1' THEN age>'3'
else THEN age>'30'
end
可以不行,谁能帮 我学一个带条件判断的类似的语句啊

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧