我在Symfony2中获得了以下学说查询:
$query = $em
->createQuery("
SELECT m FROM MyBackendBundle:Merchant m
WHERE m.active = :active
ORDER BY FIND_IN_SET(m.range, 'all', 'without_special'), m.tradingAmount DESC
")
->setParameter('active', true)
;
但这会导致以下错误:
[Syntax Error] line 0, col 112: Error: Expected end of string, got '('
和
QueryException:
SELECT m FROM My\Bundle\BackendBundle\Entity\Merchant m
WHERE m.active = :active
ORDER BY FIND_IN_SET(m.range, 'all', 'without_special') ASC, m.tradingAmount DESC
我使用FIND_IN_SET
doctrine extension from beberlei可以在查询中使用它。
为什么会发生这种情况?
更新
使用SELECT中的FIND_IN_SET
作为别名:
SELECT m, FIND_IN_SET(m.range, 'all', 'without_special') AS HIDDEN findInSet
FROM MyBackendBundle:Merchant
WHERE m.active = :active
ORDER BY findInSet, m.productAmount DESC
导致以下错误:
[Syntax Error] line 0, col 56: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','
答案 0 :(得分:1)
感谢@qooplmao的评论,这是工作版本:
$query = $em
->createQuery("
SELECT m, FIND_IN_SET(m.range, 'all,without_special') AS rangeOrdering
FROM MyBackendBundle:Merchant m
WHERE m.active = :active
ORDER BY rangeOrdering, m.tradingAmount DESC
")
->setParameter('active', true)
;
FIND_IN_SET
只有两个参数,必须位于查询的SELECT
部分,因为函数无法在查询的ORDER BY
部分使用。