如何在postgres中实现sql

时间:2015-03-26 02:01:05

标签: php postgresql

我的手臂是实现sql:

步骤1: 设置变量dev:

\set dev '(select COALESCE(STDDEV_POP(answer_count+created_at/600000), 0) from question_view where created_at > {$limitTime})'

步骤2: 得到最热门的问题清单:

SELECT id, floor(q.answer_count+add_time/600000-@dev) as sort from question_view WHERE state in (1, -1) and add_time > {$limitTime} order by sort

我该怎么做php?

$sql = "
        \set dev '(select COALESCE(STDDEV_POP(answer_count+created_at/600000), 0) from question_view where created_at > {$limitTime})'
        ";
        $pdo->exec($sql);

        $sql = "
        SELECT id, floor(q.answer_count+add_time/600000-@dev) as sort from question_view WHERE state in (1, -1) and add_time > {$limitTime}
        ";

        if ($max) {
            $sql .= " and sort < {$max} ";
        }

        $sql .= " ORDER BY id DESC
            OFFSET {$offset}
            LIMIT {$limit}";

        $stm = $pdo->prepare($sql);
        $stm = $stm->execute();

但代码不起作用~~

0 个答案:

没有答案