SQL Update根据另一个表中的条件更新一个表

Jos*_*ond 12 sql sql-update

两张桌子.

Content (table),
   topic_id (primary key),
   data (text)

Topics (table),
   topic_id (primary key),
   content_type (text)
Run Code Online (Sandbox Code Playgroud)

两个表都具有相同的主键数据(topic_id).

我需要使用"禁用"文本更新数据字段(内容表),但仅限于content_type字段(主题表)=文本"rvf"的位置

我可以: SELECT * from topics WHERE content_type = "rvf";

我可以: UPDATE content SET data = ("disabled");

但我怎么能把它们放在一起呢.

a_h*_*ame 25

标准ANSI SQL解决方案(应该适用于任何DBMS)

UPDATE content 
   SET data = 'disabled'
 WHERE topic_id IN (SELECT t.topic_id 
                    FROM topics t
                    WHERE t.content_type = 'rvf')
Run Code Online (Sandbox Code Playgroud)


Abe*_*ler 8

如果您使用SQL Server,这应该工作

UPDATE content 
SET data = 'disabled'
FROM content
INNER JOIN topics
on content.topic_id = topics.topic_id
WHERE content_type = 'rvf'
Run Code Online (Sandbox Code Playgroud)

您还可以通过执行以下操作来更新包含主题值的内容:

UPDATE content 
SET content.data = topics.content_type
FROM content
INNER JOIN topics
on content.topic_id = topics.topic_id
WHERE content_type = 'rvf'
Run Code Online (Sandbox Code Playgroud)

不确定它是否适用于这种情况,但很高兴知道你可以......