我现在有两张表
A表
name id address
1 2 四川
2 3 云南
3 4 贵州B表
name id address
11 2 成都
22 3 昆明现在我需要做的是,将B的address 前面加上 与其ID相对应的A表的address结果希望是这样
B表
name id address
11 2 四川成都
22 3 云南昆明请教一下,这条Sql该怎么写,还有就是如果要学习这一部分的知识,需要看什么资料,希望大家推荐一下。
(本来想弄200分的....提示发不了....)
A表
name id address
1 2 四川
2 3 云南
3 4 贵州B表
name id address
11 2 成都
22 3 昆明现在我需要做的是,将B的address 前面加上 与其ID相对应的A表的address结果希望是这样
B表
name id address
11 2 四川成都
22 3 云南昆明请教一下,这条Sql该怎么写,还有就是如果要学习这一部分的知识,需要看什么资料,希望大家推荐一下。
(本来想弄200分的....提示发不了....)
解决方案 »
- 各路神仙,小弟刚接触oracle,问个简单的问题。
- 请教OFS和RAC有什么区别?
- 数据库移机 IMP导入时出现ORA-03212:无法在本地管理的表空间创建临时段!
- 急!!!如何用触发器记录用户执行动作???
- 字符集真的能把我逼疯,求救啊~~
- 如何让一个job在每个星期第一天的凌晨1点自动执行
- 从oracle导数据导sqlserver中,某些权限如system/sys下的表在sqlserver的dts中看不见
- 请教:从sql server2000导出数据到oracle8i,表名是大小写混写的,到oracle也是,所以出了问题。
- 表中的记录存在从属关系,sql语句该如何写,精通sql的高手一定要来帮帮忙,我的最后一关!
- win32安装occi12客户端,createEnvironment出错?
- oracle 11gR2安装
- oracle存储过程返回数组问题,菜鸟问题,麻烦看下,在线等
where aa.id = bb.id;
where aa.id = bb.id(+)--买一本入门书籍,然后多练习!
create table test as select bb.name, bb.id, aa.address||bb.address address from
A aa, B bb
where aa.id (+)=bb.id;
Second
rename B to test1;
Third
rename test to B;
Finally
drop table test1;
update B
set address=aa..address||bb.address
from A aa join B bb
on aa.id = bb.id ;
为保证“还有就是B表中,包含有许多和A表对应不上的数据,那一部分数据,希望在做了我上面说的操作后任然保留。”,a、b两表数据调换,a两条,b三条select b.name, b.id,
b.address || a.address -- 基本的字符串连接
from (select 1 name, 2 id, '四川' address from dual
union
select 2 name, 3 id, '云南' address from dual
union
select 3 name, 4 id, '贵州' address from dual
) b, -- 用union将各条数据放在一起当做b表
(select 11 name, 2 id, '成都' address from dual
union
select 22 name, 3 id, '昆明' address from dual
) a
where b.id = a.id(+) -- 前边表(b)的全部数据都保留看基础的,有课本的话看课本就可以
where aa.id = bb.id(+))
where exists(select 1 from A aa, B bb
where aa.id = bb.id(+))
select b.name, b.id, b.address || a.address "address"
from a,b
where b.id = a.id(+)
接分,建议楼主看看 oracle的基础与提高吧.
此法不妥,可用update啊
楼主说的“还有就是B表中,包含有许多和A表对应不上的数据,那一部分数据,希望在做了我上面说的操作后任然保留。”B表中许多和A表对应不上的(id不等??)数据压根就不会被update到,这个只会更新和A表id相对应的数据。
set address=aa..address||bb.address
from A aa join B bb
on aa.id = bb.id ;
update b set address = (select address from a where a.id = b.id)||address
要是不是太大表可以如此:
update b set address = (select a.address||b.address from a where a.id = b.idwhere id in (select id from a )
哈哈,这样行不update b set address = nvl((select address from a where a.id = b.id),'')||address
--教你个方法,顺便还能学习到一个知识点
merge into B
using A
on(B.id = A.id)
when matched then
update set B.address = A.address||B.address;--方法二
update B
set B.address = B.address||(select A.address from A where A.id=B.id)
where exists(select 1 from from A where A.id=B.id);--个人建议你使用方法一。
--用这个吧,你还能学到新的知识点。
merge into B
using A
on(B.id=A.id)
when matched then
update set B.address = A.address || B.address;