'Update postgres field with results from another query

I have table with following format.

Moderations table
+---------+------------+--------------+
| mod_id  | post_id    | is_moderated |
+---------+------------+--------------+
|  1      |    1       |    true      |
+---------+------------+--------------+
|  2      |    2       |    false     |
+---------+------------+--------------+
|  1      |    3       |    false     |
+---------+------------+--------------+
|  3      |    4       |    false     |
+---------+------------+--------------+
|  4      |    5       |    false     |
+---------+------------+--------------+

I have another table that tracks whether a particular mod is active.

Moderator Table
+---------+---------------+
| mod_id  | is_mod_active |
+---------+---------------+
|  1      |     true      |
+---------+---------------+
|  2      |     false     |
+---------+---------------+
|  3      |     false     |
+---------+---------------+
|  4      |     true      |
+---------+---------------+

I perform following query to get the moderation requests that are unmoderated, and for whom assigned moderator has gone inactive.

SELECT * 
from 
    moderation 
left join moderator 
    on mod_id=user_id 
where 
    is_mod_active = false and 
    is_moderated = false
order by 
created_on

for these selected rows I would like to update the mod_id field with following query

SELECT mod_id 
FROM 
    moderator
WHERE
    is_mod_active = true

The second query will be shorter or longer than the first query. Is there a way to update this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source