日期:2014-05-17  浏览次数:20524 次

SQL语句怎么写才能不会又臭又长
SQL code

select  case when col1 = 1 then "1" 
        else when col1 = 2 then "2" 
        else when col1 = 3 then "3" 
from table1
where  (case when col = 1 then 1 
       else when col = 2 then 2 
       else when col = 3 then 3 )> col2


上述sql语句有更加好的写法吗,上面的语句一旦case when ... else when 多了之后,又臭又长

------解决方案--------------------
根据LZ给的例子,select中的case语句肯定是少不了的,要优化也是where语句里的case,如下
SQL code

select * from
(
 select  case when col1 = 1 then "1" 
         else when col1 = 2 then "2" 
         else when col1 = 3 then "3" end condi,*
 from table1
)t
where condi>col2