select一个as 语句的where 条件

vchalf_moon 2011-06-28 04:05:26
select (case when a.djzt=1 or a.djzt=2 then 1 else 0 end) as ck from table a where ck=1;
上面这句是会错的
除了
select (case when a.djzt=1 or a.djzt=2 then 1 else 0 end) as ck from table a where a.djzt=1 or a.djzt=2 ;

select * from (
select (case when a.djzt=1 or a.djzt=2 then 1 else 0 end) as ck from table a ) b where b=1

有没有简单一点的
...全文
683 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
vchalf_moon 2011-07-06
  • 打赏
  • 举报
回复
select (case when a.djzt=1 or a.djzt=2 then 1 else 0 end) as ck from table a where ck=1
其实省掉了其他字段,本意是想修改 where ck=1 这个条件 来得到对应记录
cd731107 2011-06-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 maco_wang 的回复:]
不理解楼主得到一列都是1 有什么用?
[/Quote]
对的,这是一个恒等式,除非是没有符合条件的记录,否则永远等于1,不会等于0
Lay 2011-06-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 vchalf_moon 的回复:]
select (case when a.djzt=1 or a.djzt=2 then 1 else 0 end) as ck from table a where ck=1;
上面这句是会错的
除了
select (case when a.djzt=1 or a.djzt=2 then 1 else 0 end) as ck from table a where a.djzt=1 or a……
[/Quote]
首先出错是where后面用别名是不对的,直接where a.djzt=1就可以了
后面where a.djzt in (1,2)和a.djzt between 1 and 2都可以用,效率都差不多
叶子 2011-06-28
  • 打赏
  • 举报
回复
不理解楼主得到一列都是1 有什么用?
老潘 2011-06-28
  • 打赏
  • 举报
回复

select 1 as ck from table where djzt in (1,2)
select 1 as ck from table where djzt between 1 and 2
select 1 as ck from table where djzt=1 or djzt=2
xuexiaodong2009 2011-06-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zy112429 的回复:]

SQL code
select 1 as ck from table a where a.djzt=1 in(1,2)
[/Quote]同意,简单啊
vchalf_moon 2011-06-28
  • 打赏
  • 举报
回复
方法也不错
GoAwayZ 2011-06-28
  • 打赏
  • 举报
回复
select 1 as ck from table a where a.djzt=1 in(1,2)

34,575

社区成员

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

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