Home  >  Article  >  Database  >  mysql仿oracle的decode效果查询

mysql仿oracle的decode效果查询

WBOY
WBOYOriginal
2016-06-07 18:03:40816browse

今天遇到群里发的一个问题,觉得有点意思,发上来看一下

以下代码在MYSQL中测试通过,MSSQL应该能跑通,未测试。
代码如下:
#创建表如下
create temporary table tmp (a int, b int ,c int);
insert into tmp VALUES (1,10,1),(10,10,2),(10,100,2);
#mysql执行
select sum(case when c = '1' then A else B end) from tmp
#oracle执行
select sum(decode(c,'1',a,b)) from tmp
#普通联合查询
select sum(d) from
(
select a as d from tmp where c=1
union
select b as d from tmp where c=2
)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn