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

整个表的数据列出来的问题?
求一SQL:
我想将整个表的数据列出来,tabletemp表中有id,flag,a,b,c,f字段,如果 flag='是' 执行 select a,b,b+c from tabletemp 如果flag='否' 执行 select a,b,b+f from tabletemp 列出全部表中的tabletemp中的数据并按id排序,这个语句怎么写?

------解决方案--------------------
select a,b,b+c from tabletemp where flag = '是'
union all
select a,b,b+f from tabletemp where flag = '否'

------解决方案--------------------
SQL code
select a , b , newcol = b+c from tabletemp where flag = '是' 
union  all 
select a , b , newcol = b+f from tabletemp where flag = '否'
order by id

------解决方案--------------------
select a,b,b+c from tabletemp where flag='是' 
union
select a,b,b+c from tabletemp flag='否' 
order by id
------解决方案--------------------
SQL code
--一共三个,你看哪个合适?
select a , b , newcol from
(
  select id , a , b , newcol = b+c from tabletemp where flag = '是' 
  union  all 
  select id , a , b , newcol = b+f from tabletemp where flag = '否'
) t
order by id

------解决方案--------------------
select a , b , newcol = b+ case when flag = '是' then c else f end
from tabletemp
------解决方案--------------------
select a , b , newcol = b+ case when flag = '是' then c else f end
from tabletemp
order by id