문제

My query:

SELECT c FROM MyBundle:Category c ORDER BY c.name = 'Archive', c.createdAt DESC

I get

Error: Expected end of string, got '='

I want all entries to be orded by date except the archive which should be the last. Any ideas what I am doing wrong?

도움이 되었습니까?

해결책

This will sort records in descending order by date, keeping the Archive records at the bottom:

SELECT c 
FROM MyBundle:Category c 
ORDER BY case when c.name = 'Archive' then 2 else 1 end, 
    c.createdAt DESC

SQL Fiddle Example

As an extension to this answer, this is what I had to change to make it work with DQL:

SELECT c,
  CASE WHEN c.name = 'Archive' THEN 2 ELSE 1 END AS HIDDEN sortCondition
  FROM MyBundle:Category c ORDER BY sortCondition, c.createdAt DESC
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top