表一
ID NAME VALUE FLAG
001 A 200 0
002 B 300 1
003 C 400 1
004 D 200 0
005 E 300 0
表二
ID NAME VALUE
002 B 310
003 C 100
如果表一中得flag为1,vaule取表二中得值
即 得到结果:
ID NAME VALUE FLAG
001 A 200 0
002 B 310 1
003 C 100 1
004 D 200 0
005 E 300 0
查询语句怎么写啊
9 个解决方案
#1
select a.id,a.name,decode(a.flag,1,b.vale,a.value) as value,a.flag
from A a,B b
where a.id = b.id
from A a,B b
where a.id = b.id
#2
with t1 as
(
select '001' as id, 'A' as name, 200 as value, 0 as flag from dual
union all
select '002' as id, 'B' as name, 300 as value, 1 as flag from dual
union all
select '003' as id, 'C' as name, 400 as value, 1 as flag from dual
union all
select '004' as id, 'D' as name, 200 as value, 0 as flag from dual
union all
select '005' as id, 'E' as name, 300 as value, 0 as flag from dual
),
t2 as
(
select '002' as id, 'B' as name, 310 as value from dual
union all
select '003' as id, 'C' as name, 100 as value from dual
)
select t1.id, t1.name,
decode(t1.flag, 1, t2.value, t1.value) as value,
t1.flag
from t1 left join t2
on t1.id = t2.id
order by t1.id;
#3
你这个会丢数据
#4
select a.ID,a.NAME,decode(a.FLAG,1,b.VALUE,a.VALUE) as VALUE,a.FLAG
from tab1 a,tab2 b
where a.ID=b.ID
#5
加个左连
select a.ID,a.NAME,decode(a.FLAG,1,b.VALUE,a.VALUE) as VALUE,a.FLAG
from tab1 a,tab2 b
where a.ID=b.ID(+)
#6
左连接看需要吧。。。主要是decode这个才是LZ需要的- -|||
#7
有没有性能高点儿的,数据量太大
#8
你这也没其他筛选条件,就等于两表数据的乘积,没什么优化了
两张表的ID都建索引了吗?
两张表的ID都建索引了吗?
#9
嗯
先这样吧,谢谢
#1
select a.id,a.name,decode(a.flag,1,b.vale,a.value) as value,a.flag
from A a,B b
where a.id = b.id
from A a,B b
where a.id = b.id
#2
with t1 as
(
select '001' as id, 'A' as name, 200 as value, 0 as flag from dual
union all
select '002' as id, 'B' as name, 300 as value, 1 as flag from dual
union all
select '003' as id, 'C' as name, 400 as value, 1 as flag from dual
union all
select '004' as id, 'D' as name, 200 as value, 0 as flag from dual
union all
select '005' as id, 'E' as name, 300 as value, 0 as flag from dual
),
t2 as
(
select '002' as id, 'B' as name, 310 as value from dual
union all
select '003' as id, 'C' as name, 100 as value from dual
)
select t1.id, t1.name,
decode(t1.flag, 1, t2.value, t1.value) as value,
t1.flag
from t1 left join t2
on t1.id = t2.id
order by t1.id;
#3
你这个会丢数据
#4
select a.ID,a.NAME,decode(a.FLAG,1,b.VALUE,a.VALUE) as VALUE,a.FLAG
from tab1 a,tab2 b
where a.ID=b.ID
#5
加个左连
select a.ID,a.NAME,decode(a.FLAG,1,b.VALUE,a.VALUE) as VALUE,a.FLAG
from tab1 a,tab2 b
where a.ID=b.ID(+)
#6
左连接看需要吧。。。主要是decode这个才是LZ需要的- -|||
#7
有没有性能高点儿的,数据量太大
#8
你这也没其他筛选条件,就等于两表数据的乘积,没什么优化了
两张表的ID都建索引了吗?
两张表的ID都建索引了吗?
#9
嗯
先这样吧,谢谢