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

时间:2021-01-23 23:06:53

Two tables.

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

Topics (table),
   topic_id (primary key),
   content_type (text)

Both tables have the same primary key data (topic_id).

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

I need to update the data field (Content table) with the text "disabled" but only where the content_type field (Topics table) = the text "rvf"

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

I can: SELECT * from topics WHERE content_type = "rvf";

我可以:从主题WHERE content_type =“rvf”中选择SELECT *;

I can: UPDATE content SET data = ("disabled");

我可以:UPDATE内容SET data =(“disabled”);

But how can I put those together.

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

2 个解决方案

#1


22  

Standard ANSI SQL solution (should work on any DBMS)

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

UPDATE content 
   SET data = 'disabled'
 WHERE topic_id IN (SELECT t.topic_id 
                    FROM topics t
                    WHERE t.content_type = 'rvf')

#2


6  

This should work if you are using SQL Server

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

UPDATE content 
SET data = 'disabled'
FROM content
INNER JOIN topics
on content.topic_id = topics.topic_id
WHERE content_type = 'rvf'

You can also update content with a value from topics by doing something like this:

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

UPDATE content 
SET content.data = topics.content_type
FROM content
INNER JOIN topics
on content.topic_id = topics.topic_id
WHERE content_type = 'rvf'

Not sure if it applies in this case, but it's good to know you can...

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

#1


22  

Standard ANSI SQL solution (should work on any DBMS)

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

UPDATE content 
   SET data = 'disabled'
 WHERE topic_id IN (SELECT t.topic_id 
                    FROM topics t
                    WHERE t.content_type = 'rvf')

#2


6  

This should work if you are using SQL Server

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

UPDATE content 
SET data = 'disabled'
FROM content
INNER JOIN topics
on content.topic_id = topics.topic_id
WHERE content_type = 'rvf'

You can also update content with a value from topics by doing something like this:

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

UPDATE content 
SET content.data = topics.content_type
FROM content
INNER JOIN topics
on content.topic_id = topics.topic_id
WHERE content_type = 'rvf'

Not sure if it applies in this case, but it's good to know you can...

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