日期:2014-05-18  浏览次数:20577 次

关于select case的问题,希望大家给看看,谢谢各位了!!
SELECT     a.*,b.name     from     chanpin     as     a,customer     as     b        
where         a.jianyan_riqi <=getdate()     and    

**************************************************************************    
大家给看看下面这段sql(case     when     )可以嵌到     这吗,另外我的sql语法有问题吗    
我现在对这sql有点乱,谢谢大家帮忙指点一下!!!!!!!!!!!    
-------------------------    
 
case     when     datediff(yy,getdate(),a.shengcan_riqi)> 12     then        
'datediff(dd,getdate(),DATEADD(yy,     3,     a.jianyan_riqi))> 15 " '    
else    
'datediff(dd,getdate(),DATEADD(yy,     4,     a.jianyan_riqi))> 15 '    
end     as     Category                         //这个Category是我随便写的对吗    
 
------------------------    
*************************************************************************
order     by     bianhao



------解决方案--------------------
应该没有错误
你在查询分析器中运行下不就知道有没有语法错误了
------解决方案--------------------
SELECT a.*,b.name from chanpin as a,customer as b
where a.jianyan_riqi <=getdate() and case when datediff(yy,getdate(),a.shengcan_riqi)> 12 then
datediff(dd,getdate(),DATEADD(yy, 3, a.jianyan_riqi))> 15
else datediff(dd,getdate(),DATEADD(yy, 4, a.jianyan_riqi))> 15
end AND 關聯條件
還少了兩個表得關聯條件

------解决方案--------------------
是不是想这样?
(case when datediff(yy,getdate(),a.shengcan_riqi)> 12 then
datediff(dd,getdate(),DATEADD(yy, 3, a.jianyan_riqi))
else
datediff(dd,getdate(),DATEADD(yy, 4, a.jianyan_riqi))
end) > 15
------解决方案--------------------
SELECT a.*, b.name from chanpin as a,customer as b
where a.jianyan_riqi <= getdate() and
case when datediff(yy, getdate(), a.shengcan_riqi) > 12
then datediff(dd, getdate(), DATEADD(yy, 3, a.jianyan_riqi))
else
datediff(dd,getdate(), DATEADD(yy, 4, a.jianyan_riqi))
end > 15 //case 返回的是值
order by bianhao