遇到个sql的题目,请帮忙做一下
两张表 t1(a,b),t2(a,c) 括号里表示字段
t1的b列有数据,t2的c列没有数据
现在要根据 t1.a=t2.a 为t2.c加入对应的t1.b数据 这个我之前有问过
很多人给我这个答案:
update t2
set c = t1.b
from t2 , t1
where t2.a = t1.a我在mysql和oracle环境下都执行过
都会报错的,报错位置在from这里而我改为
update t1,t2
set t2.c = t1.b
where t2.a = t1.a
在mysql下可以正常执行且结果正确
而在oracle下面仍然报错,位置在首行的逗号那里请帮帮忙
这个也许是个简单的问题,但是困扰我很久了
谢谢~~
两张表 t1(a,b),t2(a,c) 括号里表示字段
t1的b列有数据,t2的c列没有数据
现在要根据 t1.a=t2.a 为t2.c加入对应的t1.b数据 这个我之前有问过
很多人给我这个答案:
update t2
set c = t1.b
from t2 , t1
where t2.a = t1.a我在mysql和oracle环境下都执行过
都会报错的,报错位置在from这里而我改为
update t1,t2
set t2.c = t1.b
where t2.a = t1.a
在mysql下可以正常执行且结果正确
而在oracle下面仍然报错,位置在首行的逗号那里请帮帮忙
这个也许是个简单的问题,但是困扰我很久了
谢谢~~
update t2
set c = (SELECT t1.b
from t2 , t1
where t2.a = t1.a)
--这个SQL SERVER的写法.
update t2
set c = t1.b
from t2 , t1
where t2.a = t1.a
--oracle中的写法为:
update t2 set c = nvl((select t1.b from t1 where t1.a = t2.a),t2.c)
其他相关内容见下:
两表数据关联的UPDATE表tb1及数据如下:
ID A B
---------- ---------- ----------
1 10 20
2 1 2
3 3 2表tb2及数据如下:
ID A B
---------- ---------- ----------
1 10 20
2 10 20根据表tb2的id更新表tb1对应的a,b的值。update tb1 set (a,b) = (select a,b from tb2 where id = tb1.id) where id in (select distinct id from tb2) ID A B
---------- ---------- ----------
1 10 20
2 10 20
3 3 2
---------------------------------------------------------------------------------------------------------
表tb2及数据如下:
ID A B
---------- ---------- ----------
1 10 20
2 10 20
3 3 2
1 20 40
2 20 40根据ID分组求A,B的和,然后更新表tb1update tb1 set (a,b) = (select sum(a),sum(b) from tb2 where id = tb1.id) where id in (select distinct id from tb2) ID A B
---------- ---------- ----------
1 30 60
2 30 60
http://topic.csdn.net/u/20081002/00/f8d90ba2-e2bb-412a-a0c5-1b6d518fc22a.html
update t2
set c = (SELECT t1.b
from t1
where t2.a = t1.a)