3

这不断出现错误,我无法弄清楚为什么......

SELECT
  i.user_id
FROM (SELECT
    SUM(us.score) AS score_sum,
    us.user_id
      FROM user_scores us
      WHERE us.created >= DATE(DATE_SUB(NOW(),INTERVAL 7 DAY))
      GROUP BY us.user_id) AS i
ORDER BY i.score_sum
LIMIT 1

带有消息的异常“CDbException”

CDbCommand 未能执行 SQL 语句:CDbCommand 未能准备 SQL 语句:SQLSTATE[HY000]:一般错误:1 接近“7”:语法错误。

执行的 SQL 语句是:

SELECT
  i.user_id
FROM (SELECT
    SUM(us.score) AS score_sum,
    us.user_id
      FROM user_scores us
      WHERE us.created >= DATE(DATE_SUB(NOW(),INTERVAL 7 DAY))
      GROUP BY us.user_id) AS i
ORDER BY i.score_sum
LIMIT 1



<?php

class WinnerCommand extends CConsoleCommand
{
    public function actionSelect()
    {
        $sql = "SELECT i.user_id FROM
            (SELECT SUM(us.score) AS score_sum, us.user_id FROM user_scores us
            WHERE us.created >= DATE(DATE_SUB(NOW(),INTERVAL 7 DAY))
            GROUP BY us.user_id) AS i
            ORDER BY i.score_sum
            LIMIT 1";
        $user_id = Yii::app()->db->createCommand($sql)->queryScalar();
        echo $user_id;
        return $user_id;
    }

}

?>

4

1 回答 1

3

我无法解释这个问题,但您可以消除表达式中的子查询。也许这会有所帮助:

        SELECT us.user_id
        FROM user_scores us
        WHERE us.created >= DATE(DATE_SUB(NOW(), INTERVAL 7 DAY))
        GROUP BY us.user_id)
        ORDER BY SUM(us.score)
        LIMIT 1
于 2013-03-02T19:32:59.453 回答