从 table 中获取一个值(使用条件)并在一个命令中存储在另一个 table 中

Get a value from a table (using a criteria) and store in another table in one command

我尝试用满足另一个 table 条件的值的总和来更新 table 中的值。

在第一个命令中,我想获取共享文章的数量(这里是3):

db=# select article_id,liked,shared, COUNT(article_id) FROM social WHERE article_id > 0 GROUP BY article_id,liked,shared HAVING article_id=2087 AND shared=true;
 article_id | liked | shared | count 
------------+-------+--------+-------
       2087 | f     | t      |     3
(1 row)

我还有一个 table,我需要将这个值 (3) 放在右栏中:

db=# SELECT article_id,shared FROM articles WHERE article_id=2087;
 article_id | shared 
------------+--------
       2087 |       
(1 row)

我可以用 Python 中的两个命令来做到这一点,但我确信在一个 SQL 请求中有更好的方法来做到这一点?

编辑:

我试过这个命令:

db=# UPDATE articles SET shared=subquery.count FROM ( SELECT count(article_id) FROM social WHERE article_id=2087 AND shared=true) AS subquery WHERE article_id=2087;
UPDATE 1

db=# SELECT article_id,shared FROM articles WHERE article_id=2087;
 article_id | shared 
------------+--------
       2087 |      3
(1 row)

我只需要设置一次 article_id 就可以了。有什么建议吗?

我会将计数包装在 CTE 中,并根据 CTE 的结果进行更新:

with counts as (
  select article_id,liked,shared, COUNT(article_id) as article_count
  FROM social
  WHERE article_id > 0
  GROUP BY article_id,liked,shared
  HAVING article_id=2087 AND shared=true
)
update articles a
set shared = c.article_count
from counts c
where a.article_id = c.article_id

其他一些挑剔的事情...

  1. HAVING article_id=2087 and shared=true 最好移到 where 子句中。当然,这会起作用,但 HAVING 通常保留用于聚合函数(having count (*) > 1having sum (qty) = 0。将其移动到 where 子句的好处是您可以避免抓取和聚合结束的数据被过滤掉了。我对此不确定,但它甚至可以更好地利用索引。

  2. shared=true可以简写为shared。您可能不喜欢失去对这意味着什么的清晰度,所以接受或保留它。

  3. 第一个查询与您要更新的内容不一致...具体来说,初始查询可能 return 多个结果,因为您没有仅按 article_id -- 因此对于相同的 article_id,您可能会得到很多结果。您信任的这个构造将更新正确的构造。你是这个意思吗?

这三个建议的总结和一些假设:

with counts as (
  select article_id, COUNT(article_id) as article_count
  FROM social
  WHERE article_id > 0 and article_id=2087 AND shared
  GROUP BY article_id
)
update articles a
set shared = c.article_count
from counts c
where a.article_id = c.article_id

我意识到 article_id > 0article_id=2087 是多余的,但我假设后者是一个测试用例,您最终会删除它,以便您可以一次更新所有记录。