3

我正在使用 Golang、PostgreSQL 和 go-pg ( https://github.com/go-pg/pg )。

我需要这个查询:

WITH cte AS (
    SELECT
        "player"."id",
        "player"."created_at",
        "player"."note"
    FROM
        "players" AS "player"
    ORDER BY
        "amount" DESC,
        "id"
        LIMIT 5
    ) ( SELECT * FROM cte ) UNION ALL
    (
    SELECT
        "id",
        "created_at",
        CONCAT_WS ( '|', LAST_VALUE ( "created_at" ) OVER ( ), LAST_VALUE ( "id" ) OVER ( ) ) AS note
    FROM
        cte -- THIS IS CORRECT
    LIMIT 1
    )

而是go-pg不断给我这个:

WITH "cte" AS (
    SELECT
        "player"."id",
        "player"."created_at",
        "player"."note"
    FROM
        "players" AS "player"
    ORDER BY
        "amount" DESC,
        "id"
        LIMIT 5
    ) ( SELECT * FROM "cte" ) UNION ALL
    (
    SELECT
        "id",
        "created_at",
        CONCAT_WS ( '|', LAST_VALUE ( "created_at" ) OVER ( ), LAST_VALUE ( "id" ) OVER ( ) ) AS note
    FROM
        "players" AS "player" -- THIS IS THE PROBLEM, I need "cte" here
    LIMIT 1
    )

我正在使用这段代码:

var players []*models.Player

queryAll := r.db.Model(&players).Limit(5)
queryNotes := r.db.Model((*models.Player)(nil)).ExcludeColumn("note").Limit(1)

queryNotes.ColumnExpr("CONCAT_WS ( '|', " + lastValues + " ) AS note")

err := queryAll.WrapWith("cte").Table("cte").UnionAll(queryNotes).Select(&players)

我能做些什么来解决这个问题?

4

0 回答 0