IF语句按照Doctrine 2查询Symfony 2的条款顺序排列

时间:2013-04-01 15:38:09

标签: mysql symfony doctrine

我正在尝试做一个显示所有评论的博客,用户将能够在以前发布的评论中发表回复评论,然后在sql查询中进行排序,这样他们就会出现以便我不断收到错误。< / p>

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

给出错误:[语法错误]第0行,第515行:错误:字符串的预期结束,得到'CASE'

甚至尝试过正常的方式

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

给出错误:[语法错误]第0行,第515行:错误:字符串的预期结束,得到'('

当我在正常的sql代码(不是doctrine)中使用正常的ORDERY BY IF()时,它可以正常工作。

2 个答案:

答案 0 :(得分:6)

看起来你只是有一个错字。您的整体语法是正确的。

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

应该是

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

答案 1 :(得分:3)

AWSOME !!! 谢谢你的帮助,我刚刚得到它!

我的解决方案:

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

我在选择部分通过计算完成了订单。