通过另一个表中的列更新一个表中的列

编程入门 行业动态 更新时间:2024-10-10 01:24:22
本文介绍了通过另一个表中的列更新一个表中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有2个表"A"和"B".两者都有一个共同的列名称",并通过列"id"相互链接.表A中的名称"列为空,而表B中具有数据.我的任务是用相应的ID填充从表B到表A的该列中的所有数据.

我正在使用以下查询:

UPDATE A SET A.name = (SELECT B.name from B WHERE A.id = B.id) WHERE EXISTS (SELECT B.name from B)

我在SQL Developer中运行查询后,它就冻结了,我不得不强制关闭它.谁能告诉我查询出了什么问题?

解决方案

请尝试以下方法:

UPDATE A SET A.name = (SELECT B.name FROM B WHERE B.id = A.id AND B.name IS NOT NULL) WHERE a.name IS NULL;

由于您使用的是Oracle,因此这里是 IS NOT NULL 的参考. /p>

这里是SQL Fiddle,因此您可以使用它: sqlfiddle/#!4/a5ad0/3

I have 2 tables 'A' and 'B'. Both have a common column 'name' and linked with each other by a column 'id'. The column 'name' in the table A is empty whereas it has data in table B. My task is to fill all the data in that column from table B to table A with corresponding ids.

I'm using the following query:

UPDATE A SET A.name = (SELECT B.name from B WHERE A.id = B.id) WHERE EXISTS (SELECT B.name from B)

As soon as I run the query in SQL developer, it just freezes and i have to force close it. Can anyone tell me what's wrong with the query?

解决方案

Try this one instead:

UPDATE A SET A.name = (SELECT B.name FROM B WHERE B.id = A.id AND B.name IS NOT NULL) WHERE a.name IS NULL;

Since you're using Oracle, here's the reference for IS NOT NULL.

Here's the SQL Fiddle so that you can play with it: sqlfiddle/#!4/a5ad0/3

更多推荐

通过另一个表中的列更新一个表中的列

本文发布于:2023-07-09 21:08:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1088216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!