【问题标题】:How to identify the last row in the result set postgres?如何识别结果集中postgres的最后一行?
【发布时间】:2016-11-08 18:57:16
【问题描述】:

我正在使用 Postgres 9.5 和 golang 库 lib/pq 与数据库交互。我执行一个返回多行的选择查询,然后我使用for rows.Next() 进行迭代,无论如何我可以在 lat 记录之前停止。如果它是最后一条记录,我想在控制台上打印其他东西。类似于以下内容:

for rows.Next() {

    var id string
    err = rows.Scan(&id)
    if err != nil {
        log.Printf("Error in rows.Scan: %s\n", err)
    }

    if (row is not last) {
        fmt.Println(id + "I am not last")
    } else {
        fmt.Println(id + "I am last")
    }
}

【问题讨论】:

    标签: postgresql go


    【解决方案1】:

    你可以在 go 中实现类似 do while 循环的东西。

    notLast := rows.Next()
    for notLast {
        //... rows.Scan
        notLast = rows.next()
        if notLast {
            fmt.Println(id + "I am not last")
        } else {
            fmt.Println(id + "I am last")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多