关于两张表联合起来update

jody_go 2011-03-23 09:41:56
表table01
time group
20110310
20110311
20110312
20110313
20110314
...

表 table02
time group
20110310 1
20110311 2
20110312 2
20110313 3
20110314 4
... ...


现在想根据table02的group来更新table01的group,用time做关联,
不知道怎么写啊
...全文
239 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jody_go 的回复:]
time重复时,group还是一样的
比如time是20110310 那group就是1,
table02 update时也是1
[/Quote]

update table01
set group=(select distinct group from table02 where table02.time=table01.time)
where exists(select 1 from table02 where table02.time=table01.time)
h_flys 2011-03-23
  • 打赏
  • 举报
回复


这个能搞得出来吗,。。。
Tito10 2011-03-23
  • 打赏
  • 举报
回复
update table01 set group=(select group from table02 where table02.time=table01.time group by group)
where exists(select 1 from table02 where table02.time=table01.time)
jody_go 2011-03-23
  • 打赏
  • 举报
回复
time重复时,group还是一样的
比如time是20110310 那group就是1,
table02 update时也是1

  • 打赏
  • 举报
回复
[Quote=引用 3 楼 changhe325 的回复:]
引用 1 楼 luoyoumou 的回复:

SQL code
update table01 t1
set t1.group=(select t2.group from table02 t2 where t2.time=t1.time)
where exists(select 1 from table02 t3 where t3.time=t1.time);
不用where那句就可以吧……
[/Quote]
不可以
如果table02中没有而table01有的time 那行的group就会被更新为null

scott@YPCOST> create table dept2 as select * from dept;

Table created.

scott@YPCOST> insert into dept2 values(50,'develop','houston');

1 row created.

scott@YPCOST> commit;

Commit complete.

scott@YPCOST> select * from dept2;

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 develop houston

scott@YPCOST> update dept2 set loc=(select loc from dept where dept.deptno=dept2.deptno)
2 /

5 rows updated.

scott@YPCOST> select * from dept2;

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 develop

scott@YPCOST> rollback;

Rollback complete.

scott@YPCOST> update dept2 set loc=(select loc from dept where dept.deptno=dept2.deptno)
2 where exists(select 1 from dept where dept.deptno=dept2.deptno);

4 rows updated.

scott@YPCOST> select * from dept2;

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 develop houston
luoyoumou 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 luoyoumou 的回复:]

-- 当time值有重复值时,你想选择哪条记录呢?你得定义个标准啊?是选择group最小的?group最大的?还是group最早出现的(根据rowid)?还是group最后出现的?

-- 这些东东你自己看着办......
[/Quote]

-- 还是 group的总和?
luoyoumou 2011-03-23
  • 打赏
  • 举报
回复
-- 标准不出来,别人无法为你工作,别人不是你肚子里的蛔虫......
luoyoumou 2011-03-23
  • 打赏
  • 举报
回复
-- 当time值有重复值时,你想选择哪条记录呢?你得定义个标准啊?是选择group最小的?group最大的?还是group最早出现的(根据rowid)?还是group最后出现的?

-- 这些东东你自己看着办......
jody_go 2011-03-23
  • 打赏
  • 举报
回复
表table01
time group
20110310
20110310
20110311
20110313
20110313
20110314
...

表 table02
time group
20110310 1
20110310 1
20110311 2
20110313 3
20110313 3
20110314 4
... ...
jody_go 2011-03-23
  • 打赏
  • 举报
回复
谢谢你们了啊
呵呵
不过我弄错了,
两个表应该是这样的
表table01
time group
20110310
20110310
20110311
20110313
20110313
20110314
...

表 table02
time group
20110310 1
20110310 2
20110311 2
20110313 3
20110313 3
20110314 4
... ...
就是说time有重复的值
changhe325 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 luoyoumou 的回复:]

SQL code
update table01 t1
set t1.group=(select t2.group from table02 t2 where t2.time=t1.time)
where exists(select 1 from table02 t3 where t3.time=t1.time);
[/Quote] 不用where那句就可以吧?
update table01 t1
set t1.group=(select t2.group from table02 t2 where t2.time=t1.time)
  • 打赏
  • 举报
回复

update table01 set group=(select group from table02 where table02.time=table01.time)
where exists(select 1 from table02 where table02.time=table01.time)
luoyoumou 2011-03-23
  • 打赏
  • 举报
回复
update table01 t1
set t1.group=(select t2.group from table02 t2 where t2.time=t1.time)
where exists(select 1 from table02 t3 where t3.time=t1.time);

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧