>  기사  >  데이터 베이스  >  仿 oracle 的decode效果查询_MySQL

仿 oracle 的decode效果查询_MySQL

WBOY
WBOY원래의
2016-06-01 13:51:43937검색

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

仿 oracle 的decode效果查询_MySQL

以下代码在MYSQL中测试通过,MSSQL应该能跑通,未测试。

#创建表如下<br>create temporary table tmp (a int, b int ,c int);<br>insert into tmp VALUES (1,10,1),(10,10,2),(10,100,2);<br>#mysql执行<br>select sum(case when c = '1' then A else B end) from tmp<br>#oracle执行<br>select sum(decode(c,'1',a,b)) from tmp<br>#普通联合查询<br>select sum(d) from <br>(<br>select a as d from tmp where c=1<br> union <br>select b as d  from tmp where c=2<br>)
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.