如何在Postgresql中的select查询中使用临时变量等效于Mysql?

时间:2017-12-06 08:10:17

标签: postgresql

我正在将数据库从Mysql迁移到Postgresql,因此Iam努力在select查询中使用临时变量,因为它在':='附近给出错误。请通过以下查询以供参考...

Mysql查询:

SELECT
    @totalHours \:= FLOOR(
        TIMESTAMPDIFF(HOUR, StartDate, EndDate)
    ) AS totalHours
FROM
    tableName @expectedEnd = CASE
WHEN @totalHours = 0 THEN
    (
        act.EndDate + INTERVAL '1 DAY'
    )
ELSE
    EndDate
END AS expectedEnd @totalHours = CASE
WHEN @totalHours = 0 THEN
    (@totalHours + 24)
ELSE
    @totalHours
END AS exptotalHours;

Postgresql查询:

SELECT
    @totalHours := abs(
        extract(
            epoch
            FROM
                (StartDate - EndDate)
        ) / 3600
    ) AS totalHours
FROM
    tableName @expectedEnd = CASE
WHEN @totalHours = 0 THEN
    (EndDate + INTERVAL '1 DAY')
ELSE
    EndDate
END AS expectedEnd @totalHours = CASE
WHEN @totalHours = 0 THEN
    (@totalHours + 24)
ELSE
    @totalHours
END AS exptotalHours;

注意:在我的情况下,无法首先声明变量并在其他选择查询中重复使用该变量

我尝试过以不同的方式将价值存储在临时的vaiables中,但我不能......有人请给我一个解决方案

感谢您的帮助

0 个答案:

没有答案