【问题标题】:looping over a simple query with different values of "id"循环使用不同的 \"id\" 值的简单查询
【发布时间】:2022-09-30 22:26:00
【问题描述】:

我有一个具有不同 id 的表,我想一个一个地用于我的查询。这是我正在尝试做的伪代码:

select distinct id
from table1

-- for every distinct id above, use that \"id\" in the query below - 

select * from table2
where num = id

我想使用循环而不使用函数/过程等来做到这一点。

每次循环运行的结果都应附加到先前的结果中。

    标签: postgresql loops


    【解决方案1】:

    如果您需要追加所有记录,则无需使用循环。 您可以使用如下子查询:

    select * from table2
    where num in (select distinct id
    from table1)
    

    【讨论】:

      猜你喜欢
      • 2012-11-26
      • 2017-04-04
      • 2014-02-18
      • 1970-01-01
      • 2017-04-21
      • 2015-02-20
      • 2019-02-01
      • 2015-09-05
      • 2010-10-19
      相关资源
      最近更新 更多