JAVA求一条更新表数据的SQL语句

有两个表A和B, A表的内容是 id=67,tops=600 id=68,tops=522这两条数据,B表的内容是id=68,tops=566 想实现的是把A表中和B表ID相同的项的数据更新为B表中对应ID的项的数据 得到新的A表 id=67,tops=600 id=68,tops=566

然后我执行了下面这条语句db.execSQL("UPDATE A set tops = (select tops from B where id in(select id from B))");得到的结果却是 id=67,tops=566,id=68,tops=566 这样的!请问正确的更新语句要怎么写?

UPDATE A set tops = (select tops from B where id in(select id from A)) where id in (select id from B)

update A a set tops= (select tops from B b where id= a.id) where a.id in (select id from B) ;

如果数据量比较大的情况下:
可以优化成:
update A a set tops= (select tops from B b where id= a.id) where EXISTS (select 1 from B bb where bb.id = a.id) ;

别名,因为A和B都有id字段,取个别名数据库就可以区分取的是那个表的id了。

UPDATE A set tops = (select tops from B) where id in (select id from B);

小 a 和小b是别名
至于后面的select 1 这个1并无什么实质意义 你要高兴1千1万随便写
EXISTS 查询如果存在即为真 不存在即为假 为真时才能查出这个1 为假查不出来

MERGE INTO A
USING B ON (A.ID = B.ID)
WHEN MATCHED THEN
UPDATE
SET A.TOPS = B.TOPS
;

update A a set tops= (select tops from B b where id= a.id) where EXISTS (select 1 from B bb where bb.id = a.id) ;