insert into b (b.name) select a.name from a,b where a.id=b.id
insert into b (b.name) select a.name from a,b where a.id=b.id
以往我的想法就是写一段代码,但是对data的操作sql应该非常强大,何况这么简单的一个功能 !其实标准sql就支持的功能,它就是having子句。大部分人对where子句非常了解,但是提到having,很多人就不知了。 比如在account中查找相同selfcode的数量和值: select count(*) ,selfcode from account group by selfcode having count(*) > 1 如下的方法是不可行的,如同自己把自己提起来一样:) select count(*) ,selfcode from account where count(*) > 1
1. UPDATE B SET B.NAME=(SELECT A.NAME WHERE A.ID=B.ID) WHERE B.NAME IS NULL;2.用having子句能夠達到查找重復行的要求,如樓上所說!
如果里面有重复的字段呢?!!!!如下:表a: name id sex --------------------------- aa 001 bb 002 cc 003 cd 003表b: name id --------------------- 001 002 003问题:将表a与表b中所有id相同的name插入到表b的name中!如何写sql语句?!
重复的照样也会插入,只要a.id=b.id就可以插入 另外看你相对重复的怎么处理了
merge into b using(select * from a) on (a.id=b.id) when matched then update set name=a.name when not matched then insert into values(a.name,a.id);
我是这么写的: update tab_b set tab_b.name=(select tab_a.name from tab_b,tab_a where tab_a.id = tab_b.id and tab_a.id not in (select t.id from tab_a t,tab_a s where t.id = s.id and t.rowid <> s.rowid)) 可样写后执行就抱错,说“单条记录无法返回多行记录”,好像是这么写的,记得不太清楚了,如果单独执行里面的select都没有问题!
比如在account中查找相同selfcode的数量和值: select count(*) ,selfcode from account
group by selfcode having count(*) > 1
如下的方法是不可行的,如同自己把自己提起来一样:)
select count(*) ,selfcode from account
where count(*) > 1
UPDATE B
SET B.NAME=(SELECT A.NAME WHERE A.ID=B.ID)
WHERE B.NAME IS NULL;2.用having子句能夠達到查找重復行的要求,如樓上所說!
---------------------------
aa 001
bb 002
cc 003
cd 003表b: name id
---------------------
001
002
003问题:将表a与表b中所有id相同的name插入到表b的name中!如何写sql语句?!
另外看你相对重复的怎么处理了
using(select * from a)
on (a.id=b.id)
when matched then
update set name=a.name
when not matched then
insert into values(a.name,a.id);
update tab_b
set tab_b.name=(select tab_a.name from tab_b,tab_a
where tab_a.id = tab_b.id and tab_a.id not in
(select t.id from tab_a t,tab_a s
where t.id = s.id and t.rowid <> s.rowid))
可样写后执行就抱错,说“单条记录无法返回多行记录”,好像是这么写的,记得不太清楚了,如果单独执行里面的select都没有问题!