4

我正在尝试创建一个显示所有评论的博客,并且用户将能够在回复之前发布的评论时发表评论,然后在 sql 查询中进行排序,这样它们就会显示出来,以便我不断收到错误。

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 代码(不是教义)中使用正常的 ORDERY BY IF() 时,它工作正常。

4

2 回答 2

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
于 2013-04-01T15:42:02.123 回答
3

真棒!!!感谢您的帮助,我刚刚得到它!

我的解决方案:

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

我在选择部分通过计算进行了排序。

于 2013-04-01T17:30:29.407 回答