sqlsql-update

SQL Update one table based on conditions in another table


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).

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"

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

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

But how can I put those together.


Solution

  • Standard ANSI SQL solution (should work on any DBMS)

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