Doctrine2 Self Join, How to join on self without relationship column?

北城余情 提交于 2019-12-23 13:08:45

问题


Example mysql query:

SELECT
a1.*
FROM
agreement a1
LEFT JOIN agreement a2 on a1.agreementType = a2.agreementType and a2.id > a1.id
WHERE
a2.id is null

The purpose of the query is to get the last agreement of the type returned. There are many types and I want only a listing of each latest agreement for each type. My example query above works as expected, but no go in DQL.

How would I do this in DQL given I do not have a column that refers to itself? Note that "agreementType" is also a foreign key to a different table as well.


回答1:


Figured it out. Thought I would share.

        SELECT
            a1
        FROM
            My\Model\Agreement a1
            LEFT JOIN My\Model\Agreement a2
                WITH a1.agreementType = a2.agreementType AND a2.id > a1.id
        WHERE
            a2.id IS NULL


来源:https://stackoverflow.com/questions/18605782/doctrine2-self-join-how-to-join-on-self-without-relationship-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!