通过另一个表中的列更新一个表中的列 [英] Updating a column in one table through a column in another table

查看:117
本文介绍了通过另一个表中的列更新一个表中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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,因此您可以使用它: http://sqlfiddle.com/#!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: http://sqlfiddle.com/#!4/a5ad0/3

这篇关于通过另一个表中的列更新一个表中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆