语句如下:
insert into abc
select a.id, b.name, c.dept
from a, b, c
where b.id=a.id and b.type=1
and c.id=a.id其中在a、c、abc三个表中id都是主键,但在表b中主键是id+type+name,就是说以id和type为条件会查得多条结果,但我只需要从中任意一条记录即可插入abc表,因为abc表中id是主键。我试着用left join来关联b表,好像还是不行,请大家指点。谢谢。
insert into abc
select a.id, b.name, c.dept
from a, b, c
where b.id=a.id and b.type=1
and c.id=a.id其中在a、c、abc三个表中id都是主键,但在表b中主键是id+type+name,就是说以id和type为条件会查得多条结果,但我只需要从中任意一条记录即可插入abc表,因为abc表中id是主键。我试着用left join来关联b表,好像还是不行,请大家指点。谢谢。
解决方案 »
- win7 旗舰版32位 装oracle10g 报错
- 求助,非DBA用户如何删除同一数据库下其他用户的表内容呢?
- 创建job时会检查job中要执行的语句吗?
- oracle字段分组问题
- !!!急!!!关于ORACLE备份和导出数据的问题
- 自己写了一个Procedure,问题不少,大家帮忙挑挑错误,谢了
- 怎样每天都使序列重新置一
- Oracle SQL*Plus中通过文件-打开-打开SQL脚本怎么不执行?
- PL/SQL响应用户操作
- 请问:我想将ORCL数据库名改为ORACLE怎么改?我不想把它卸了重新创建,而且修改后包括服务里的名称也得改了。谢谢!有人能告诉我吗?了
- 批量数据INSERT问题
- 关于asp调用oracle存储过程的问题
select a.id, b.name, c.dept
from a, b, c
where b.id=a.id and b.type=1
and c.id=a.id
and rownum=1;
谢谢回复,但你这样一共只会向abc表插入一条记录,而我的意思是对应每个id 插入一条记录.
insert into abc
select a.id, b.name, c.dept
from a, b, c
where b.id=a.id and b.type=1
and c.id=a.id
and b.name in
(select name from b where rownum = 1)
樓主的意思應該要求同時能插入多條記錄
insert into abc
select a.id, b.name, c.dept
from a, b, c
where b.id=a.id and b.type=1
and c.id=a.id
and b.name in
(select name from b d where rownum = 1 and id = b.id and type = b.type)
谢谢回复,你是明白我的意思了:)方法也可行,但是如果对于a表中的一些id,如果在b表或c表中没有对应的记录,就不会向abc表插入记录,而我希望此时b.name, c.dept 以空值代替。这样最后插入到abc表中的记录总数应该和a表记录数一样多。谢谢,请多指教!
select a.id, b.name, c.dept
from a, b, c
where b.id(+)=a.id and b.type=1
and c.id(+)=a.id
and (b.name in
(select name from b d where rownum = 1 and id = b.id and type = b.type) or b.name is null)