当前位置:数据库 > MySQL >>

mysql仿oracle的decode效果查询

答案:

以下代码在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
)

上一个:mysql grants小记
下一个:ACCESS 调用后台存储过程的实现方法

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,