我有这个SQL查询在phpMyAdmin中工作正常:
SELECT `Tag_Type_Code`, `Tag_Type`,
GROUP_CONCAT(`Tag_Code` ORDER BY `Tag_Name`) as tagCode,
GROUP_CONCAT(`Tag_Name` ORDER BY `Tag_Name`) as tagName,
GROUP_CONCAT(CASE WHEN isnull(`Tag_Picto`) THEN 'NULL' ELSE `Tag_Picto` END ORDER BY `Tag_Name`) as tagPicto
FROM tags
WHERE `modele_code` = "MYCODE"
GROUP BY `Tag_Type_Code`
ORDER BY `tag_Type` ASC
我想在 Doctrine和Symfony3 中执行此操作,我尝试了这一点,但我在CASE WHEN附近有语法错误。
$this->getEntityManager()->createQuery(
'SELECT tag.typeCode, tag.type,
GROUP_CONCAT(tag.code ORDER BY tag.name) as tagCode,
GROUP_CONCAT(tag.name ORDER BY tag.name) as tagName,
GROUP_CONCAT(CASE WHEN tag.picto IS NULL THEN "NULL" ELSE tag.picto END ORDER BY tag.name) as tagPicto
FROM AppBundle:Tag tag
WHERE tag.modelCode = :code
GROUP BY tag.typeCode
ORDER BY tag.type ASC'
)->setParameter('code', $modelCode)
->getResult();
或者这个:
'SELECT tag.typeCode, tag.type,
CASE WHEN tag.picto IS NULL THEN "NULL" ELSE tag.picto END as picto
GROUP_CONCAT(tag.code ORDER BY tag.name) as tagCode,
GROUP_CONCAT(tag.name ORDER BY tag.name) as tagName,
GROUP_CONCAT(picto ORDER BY tag.name) as tagPicto
FROM AppBundle:Tag tag
WHERE tag.modelCode = :code
GROUP BY tag.typeCode
ORDER BY tag.type ASC'
我还尝试使用beberlei/DoctrineExtensions bundle中的IfElse和IfNull扩展名,但我找不到正确的语法。
此查询无法使用CASE WHEN。
感谢您的帮助。
答案 0 :(得分:0)
我认为您的问题是在"NULL"
使用双引号,使用单引号并转义它们:
$this->getEntityManager()->createQuery(
'SELECT tag.typeCode, tag.type,
GROUP_CONCAT(tag.code ORDER BY tag.name) as tagCode,
GROUP_CONCAT(tag.name ORDER BY tag.name) as tagName,
GROUP_CONCAT(CASE WHEN tag.picto IS NULL THEN \'NULL\' ELSE tag.picto END ORDER BY tag.name) as tagPicto
FROM AppBundle:Tag tag
WHERE tag.modelCode = :code
GROUP BY tag.typeCode
ORDER BY tag.type ASC'
)->setParameter('code', $modelCode)
->getResult();