简体   繁体   English

DQL学说查询翻译

[英]DQL Doctrine query translation

I have my scores table where I have multiple scores for 1 user. 我有分数表,其中有1个用户的多个分数。 What I am trying to do is to select all highest scores for each user. 我想做的是为每个用户选择所有最高分数。

I am trying to do the fallowing in Doctrine DQL: 我正在尝试在Dctrine 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

My current state is: 我目前的状态是:

    $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();

And I am getting the following error: 我收到以下错误:

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

What am I doing wrong? 我究竟做错了什么?

Try put two backslashes in the namespaces.. like: 尝试在名称空间中添加两个反斜杠。

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

if this doe's not work, try to do the query in small parts, to figure out where is the problem.. 如果此操作无效,请尝试分小部分进行查询,以找出问题所在。

This should work in DQL you have to ON part handles by doctrine if you have defined any mapping for your entities if not and still want to join 2 entities with common attribute you can use WITH clause 如果您没有为实体定义任何映射,并且仍然想加入2个具有公共属性的实体,则可以使用WITH子句,这在DQL中应该起作用,如果您没有为实体定义任何映射,则必须按原则ON零件句柄

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

OR 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM