0

我有我的分数表,其中有 1 个用户的多个分数。我要做的是为每个用户选择所有最高分。

我正在尝试在 Doctrine DQL 中进行休闲:

        SELECT * FROM scores s1
        LEFT OUTER JOIN scores s2 ON
          s1.user_id = s2.user_id
          AND ((s1.score < s2.score) OR (s1.score = s2.score AND s1.date_added < s2.date_added))
        WHERE s2.score IS NULL
        ORDER BY s1.score DESC
        LIMIT 10

我现在的状态是:

    $rowQuery = $this->getEntityManager()->createQuery('
        SELECT s1 FROM \Digital\ApplicationBundle\Entity\ChallengeScore s1
            LEFT OUTER JOIN \Digital\ApplicationBundle\Entity\ChallengeScore s2
        ON (
            s1.user = s2.user
            AND
            (s1.score < s2.score OR (s1.score = s2.score AND s1.date_added < s2.date_added))
        )
        WHERE s2.score IS NULL
        AND s1.date_added BETWEEN :monday AND :sunday
        ORDER BY s1.score DESC
    ');


    $rowQuery->setParameter('monday', $startDate->format('Y-m-d'))
             ->setParameter('sunday', $endDate->format('Y-m-d'));
    $rowQuery->setMaxResults($limit);

    return $rowQuery->getResult();

我收到以下错误:

[Syntax Error] line 0, col 188: Error: Expected Literal, got '�'

我究竟做错了什么?

4

2 回答 2

0

尝试在命名空间中放置两个反斜杠。例如:

$rowQuery = $this->getEntityManager()->createQuery('
    SELECT s1 FROM \\Digital\\ApplicationBundle\\Entity\\ChallengeScore s1
    LEFT OUTER JOIN \\Digital\\ApplicationBundle\\Entity\\ChallengeScore s2 ...

如果这不起作用,请尝试分小部分进行查询,以找出问题所在..

于 2013-04-29T20:22:42.860 回答
0

这应该在 DQL 中起作用,ON如果您已经为实体定义了任何映射,则必须按原则划分句柄,如果没有,并且仍然想加入 2 个具有公共属性的实体,您可以使用WITH子句

SELECT s1 
FROM \Digital\ApplicationBundle\Entity\ChallengeScore s1
    LEFT OUTER JOIN \Digital\ApplicationBundle\Entity\ChallengeScore s2
    WITH s1.user = s2.user
    AND CASE WHEN s1.score = s2.score
            THEN s1.date_added < s2.date_added
            ELSE s1.score < s2.score
    END
WHERE s2.score IS NULL
AND s1.date_added BETWEEN :monday AND :sunday
ORDER BY s1.score DESC

或者

SELECT s1 
FROM \Digital\ApplicationBundle\Entity\ChallengeScore s1
    LEFT OUTER JOIN \Digital\ApplicationBundle\Entity\ChallengeScore s2
    WITH s1.user = s2.user
    AND (
        s1.score < s2.score OR (s1.score = s2.score AND s1.date_added < s2.date_added)
    )
WHERE s2.score IS NULL
AND s1.date_added BETWEEN :monday AND :sunday
ORDER BY s1.score DESC
于 2017-12-05T10:23:43.990 回答