【问题标题】:offset multiple rows by id postgres通过 id postgres 偏移多行
【发布时间】:2019-04-01 17:31:22
【问题描述】:

我正在尝试从我的查询结果中更新多行,但我无法更新。

这是我的表格结果

15347   108
15665   108
15297   108
15454   105
15850   105
15304   205
15690   205
15360   205

我只想用 id_num 偏移第一个结果,例如 15347、15454 和 15304...

这是我正在使用的 sql 查询

select id, id_num from animal where club = 78 offset 1 limit all;

但它只偏移了第一行... 这就是我想要的结果

15665   108
15297   108
15454   105
15304   205
15690   205

【问题讨论】:

  • 您是什么意思“仅通过 id 偏移第一个结果”?请显示您想要的结果。
  • @GordonLinoff 抱歉,我现在编辑了我的问题。可以看看吗?

标签: sql postgresql offset


【解决方案1】:

您是说您想要每个col2 的最小值为col1?如果是这样,这与offset无关。

您的查询将是这样的:

select a.*
from animal a
where a.club = 78 and
      a.col1 = (select min(a2.col1)
                from animal a2
                where a2.col1 = a.col1 and a2.club = a.club
               );

【讨论】:

    【解决方案2】:

    你可以使用DISTINCT ON ()

    SELECT DISTINCT ON (col2) *
    FROM animal
    WHERE a.club = 78
    ORDER BY id; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-12
      相关资源
      最近更新 更多