一句 SQL 更新一张表
数据库:Oracle
表:A(id, content), B(id, content)
已知:A, B 两表的结构完全一样:id: number, content: varchar2(40).
求:通过 A.id = B.id 关联两表,用一句 SQL 将 B.content 的内容合并到 A.content 上。
说明:
假定有这样两条数据:
A(1, 'hello')
B(1, 'world')
那么执行 SQL 后,A的数据应该更新为:A(1, 'helloworld')
答案:
update a set a.content = a.content || (select b.content from b where a.id = b.id)
后记:这个问题来源于在 DV 的面试问题。