SQL根据另一个表中的条件更新一个表
两张桌子SQL根据另一个表中的条件更新一个表,sql,sql-update,Sql,Sql Update,两张桌子 Content (table), topic_id (primary key), data (text) Topics (table), topic_id (primary key), content_type (text) 两个表具有相同的主键数据(主题id) 我需要用文本“disabled”更新数据字段(Content table),但仅当Content_type字段(Topics table)=文本“rvf”时 我可以:从content\u type=
Content (table),
topic_id (primary key),
data (text)
Topics (table),
topic_id (primary key),
content_type (text)
两个表具有相同的主键数据(主题id)
我需要用文本“disabled”更新数据字段(Content table),但仅当Content_type字段(Topics table)=文本“rvf”时
我可以:从content\u type=“rvf”所在的主题中选择*代码>
我可以:更新内容集数据=(“禁用”)代码>
但是我如何才能把它们放在一起。如果您使用的是SQL Server,这应该可以工作
UPDATE content
SET data = 'disabled'
FROM content
INNER JOIN topics
on content.topic_id = topics.topic_id
WHERE content_type = 'rvf'
您还可以通过执行以下操作,使用“主题”中的值更新内容:
UPDATE content
SET content.data = topics.content_type
FROM content
INNER JOIN topics
on content.topic_id = topics.topic_id
WHERE content_type = 'rvf'
不确定它是否适用于这种情况,但很高兴知道您可以…标准ANSI SQL解决方案(应适用于任何DBMS)
这不是缺少了content\u type='rvf'的条件吗?
谢谢,工作得很好。这是在SQlite中,忘了提到这一点。
UPDATE content
SET data = 'disabled'
WHERE topic_id IN (SELECT t.topic_id
FROM topics t
WHERE t.content_type = 'rvf')