Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF Statement in order by clause of Doctrine 2 query Symfony 2

Im trying to do an blog that shows all the comments and user will be able to post comments on reply on previously posted comments no to order then in the sql query so they will show up in order i keep getting 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 Error: [Syntax Error] line 0, col 515: Error: Expected end of string, got 'CASE'

even tried normal way

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

Gives Error: [Syntax Error] line 0, col 515: Error: Expected end of string, got '('

When i use the normal ORDERY BY IF() in normal sql code (not doctrine) it works fine.

like image 255
Louwki Avatar asked Jan 14 '23 18:01

Louwki


1 Answers

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

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

should be

ORDER BY CASE WHEN (c.parentid = 0) THEN c.id ELSE c.parentid END, c.createdat ASC
like image 70
Matt Busche Avatar answered Jan 16 '23 08:01

Matt Busche