IF Approval OK on Doctrine 2 Symfony 2 Request

I am trying to make a blog that displays all the comments, and the user will be able to send comments on the responses to previously posted no comments, then order in the sql request so that they appear in order, so that I continue to get the error.

SELECT c.userid, c.comment, c.reply, u.username, c.createdat, c.image, c.username as ComUser FROM Bundle:Comments c LEFT JOIN Bundle:Users u WITH c.userid = u.id WHERE c.articleid = 1 ORDER BY (CASE WHEN (c.parentid = 0) THEN c.id ELSE c.parentid END) c.createdat ASC 

Gives an error: [Syntax error] line 0, col 515: Error: expected end of line received by "CASE"

even tried the normal way

 ORDER BY IF(c.parentid = 0, c.id, c.parentid), c.createdat ASC 

Gives an error: [Syntax error] line 0, col 515: Error: expected end of line, got '('

When I use regular ORDERY BY IF () in normal sql code (not doctrine), it works fine.

+4
source share
2 answers

it looks like you just have a typo. Your general syntax is correct.

 ORDER BY CASE WHEN (c.parentid = 0) THEN c.id ELSE .parentid END c.createdat ASC 

it should be

 ORDER BY CASE WHEN (c.parentid = 0) THEN c.id ELSE c.parentid END, c.createdat ASC 
+6
source

Awsome !!! Thanks for the help guys, I just got it!

My decision:

 SELECT c.userid, c.comment, c.reply, u.username, c.createdat, c.image, c.username as ComUser, (CASE WHEN (c.parentid = 0) THEN c.id ELSE c.parentid END) as OrderByComm FROM Bundle:Comments c LEFT JOIN Bundle:Users u WITH c.userid = u.id WHERE c.articleid = 1 ORDER BY OrderByComm, c.createdat ASC 

I made an order by calculating in the selected part.

+3
source

All Articles