首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

这个语句怎么写

2012-02-10 
这个语句如何写selectvisible,ydqxcasewhenpost_id1then4whenpost_id2then4whenui.Department_idar.De

这个语句如何写
select   visible,ydqx       =     case      
when   post_id   =1     then   4  
  when   post_id   =2   then   4  
  when   ui.Department_id=ar.Department_id     then   3
  ---when     user_id=10004               then           1
  ---when     user_id   <> 10004           then           2
else   2   end           from   userinfo     ui         ,     article     ar--
where     id=4114   and   user_id=10004
and     ydqx   > =visible

ydqx     是表中没有的列,在 'and     ydqx   > =visible '中总是报:列名   'ydqx '   无效。

不知道该如何在where   后引用   ydqx

[解决办法]
計算列不能直接拿來使用,必須寫全

select visible,ydqx = case
when post_id =1 then 4
when post_id =2 then 4
when ui.Department_id=ar.Department_id then 3
---when user_id=10004 then 1
---when user_id <> 10004 then 2
else 2 end from userinfo ui , article ar--
where id=4114 and user_id=10004
and (case
when post_id =1 then 4
when post_id =2 then 4
when ui.Department_id=ar.Department_id then 3
---when user_id=10004 then 1
---when user_id <> 10004 then 2
else 2 end) > =visible

热点排行