有3张表:
TableA(id,name) --记录地名 代码,名称。
如(01,北京);(02,上海);(03,广州);(04,沈阳)TableB(id,name) --记录国家代码,名称。
如(01,中国);(02,美国);(03,日本);(04,英国)TableC(id,type) --记录中间数据(代码,类型)
如(01,a);(02,a) ; (03,b)
其中a代表地区,b代表国家现在想 TableC 让实现如下效果:
01 , a , 北京
02 , a , 上海
03 , b , 日本不知相关的sql语句怎么写,请各位大大不吝指教啊
不知道我有没有描述清楚。
TableA(id,name) --记录地名 代码,名称。
如(01,北京);(02,上海);(03,广州);(04,沈阳)TableB(id,name) --记录国家代码,名称。
如(01,中国);(02,美国);(03,日本);(04,英国)TableC(id,type) --记录中间数据(代码,类型)
如(01,a);(02,a) ; (03,b)
其中a代表地区,b代表国家现在想 TableC 让实现如下效果:
01 , a , 北京
02 , a , 上海
03 , b , 日本不知相关的sql语句怎么写,请各位大大不吝指教啊
不知道我有没有描述清楚。
解决方案 »
- 急 在线等 “程序包或函数 INSERT_TEACHER 处于无效状态"
- 如何判断一个字段的值是否包含在某个字符串中??
- 求大神帮忙分析一个SQL,不是很复杂,但是做起来真的很难!!!
- 求一个sql语句
- 请教关于Oracle外部认证的问题!谢谢!
- 紧急求助各路ORACLE大侠,启动过程的ORA-01031的问题,多谢
- proc 文件编译问题
- 用EXP和IMP导出和导入数据时,是不是只能在数据库服务器上操作,不能在客户端操作这两个命令吗?
- 如何将CLOB类型字段的值转到VARCHAR2类型的字段中?
- merge into 越来越慢,求教大家
- Oracle安装中的问题,救急啊
- 急!oracle11g第二版卸载问题
with tba as(
select '01' id,'北京' name from dual union all
select '02','上海' from dual union all
select '03','广州' from dual union all
select '04','沈阳' from dual),
tbb as(
select '01' id,'中国' name from dual union all
select '02','美国' from dual union all
select '03','日本' from dual union all
select '04','英国' from dual),
tbc as(
select '01' id,'a' type from dual union all
select '02','a' from dual union all
select '03','b' from dual)
--以上为提供数据,相当于你的表
select tbc.id,tbc.type,tba.name
from tbc,tba
where tbc.id=tba.id
and tbc.type='a'
union all
select tbc.id,tbc.type,tbb.name
from tbc,tbb
where tbc.id=tbb.id
and tbc.type='b'ID T NAME
-- - ----
01 a 北京
02 a 上海
03 b 日本
select c.id,c.type,a.name from TableC c ,TableA a where 条件
union all
select c.id,c.type,b.name from TableC c ,TableB b where 条件不用union all的话select c.id,c.type,(a.name|b.name) from TableC c ,TableA a ,TableB B where 条件
这个语句怎么实现呢,我也想知道
with tba as(
select '01' id,'北京' name from dual union all
select '02','上海' from dual union all
select '03','广州' from dual union all
select '04','沈阳' from dual),
tbb as(
select '01' id,'中国' name from dual union all
select '02','美国' from dual union all
select '03','日本' from dual union all
select '04','英国' from dual),
tbc as(
select '01' id,'a' type from dual union all
select '02','a' from dual union all
select '03','b' from dual)
select tbc.id,tbc.type,decode(tbc.type,'a',tba.name,tbb.name)
from tbc,tba,tbb
where (tbc.id=tba.id and tbc.id=tbb.id and tbc.type='a')
or (tbc.id=tba.id and tbc.id=tbb.id and tbc.type='b')ID T DECO
-- - ----
01 a 北京
02 a 上海
03 b 日本
'a' AS TYPE,
city_name AS NAME
FROM city
UNION ALL
SELECT country_id AS ID,
'b' AS TYPE,
country_name AS NAME
FROM country
-- result:
ID TYPE NAME
-------- ---- ----------
01 a 北京
02 a 上海
03 a 广州
01 b 中国
2 select '01' id,'北京' name from dual union all
3 select '02','上海' from dual union all
4 select '03','广州' from dual union all
5 select '04','沈阳' from dual),
6 tb2 as(
7 select '01' id,'中国' name from dual union all
8 select '02','美国' from dual union all
9 select '03','日本' from dual union all
10 select '04','英国' from dual),
11 tb3 as(
12 select '01' id,'a' type from dual union all
13 select '02','a' from dual union all
14 select '03','b' from dual)
15 select b.id,b.type,decode(b.type,'a',a.name,'b',c.name) name
16 from tb1 a,tb3 b,tb2 c
17 where c.id=b.id and a.id=b.id
18 /
ID TYPE NAME
-- ---- ----
01 a 北京
02 a 上海
03 b 日本
select c.id,c.type,nvl(b.name,c.name)
from tablec c
left join tablea a on c.id=a.id and c.type='a'
left join tableb b on c.id=b.id and c.type='b'
;
select '01' id,'北京' name from dual union all
select '02','上海' from dual union all
select '03','广州' from dual union all
select '04','沈阳' from dual),
tbb as(
select '01' id,'中国' name from dual union all
select '02','美国' from dual union all
select '03','日本' from dual union all
select '04','英国' from dual),
tbc as(
select '01' id,'a' type from dual union all
select '02','a' from dual union all
select '03','b' from dual)
select tbc.id,tbc.type,decode(tbc.type,'a',tba.name,'b',tbb.name,null)
from tbc,tba,tbb
where tbc.id=tba.id and tbc.id=tbb.id
select c.id,c.type,decode(c.type,'a',a.name,'b',b.name) name
from tableA a,tableB b,tableC c
where c.id=b.id and a.id=b.id