我有这个问题:
select -- fields--
from myTable
where -- conditions --
order by myField if('param'='1' ,'desc','')
if('param'='1' ,'desc',''):根据该参数,我想按升序或降序排序。 执行这个查询,我得到这个错误:
check the manual that corresponds to your MySQL server version for the right syntax to use near 'if('param'='1' ,'desc','')'
最佳答案
类似的东西?
select -- fields--
from myTable
where -- conditions --
order by CASE WHEN @param=1 THEN myField ELSE -myField END;
如果通常在存储过程中使用,则必须如上所示使用 CASE/WHEN。此外,'param' 没有意义,您可能想要 @param。最后,不可能有条件地设置 ASC/DESC,但可以通过取字段值的负数/正数来实现相同的目标。
关于mysql - 根据参数选择升序或降序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41120037/