2016-09-12 35 views
0

我有这样的SQL查询其在phpMyAdmin正常工作:学说:GROUP_CONCAT的CASE语法是什么?

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 

而且我想这样做在教义和Symfony3,我试过,但我有情况下,当附近有语法错误。

$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' 

我还试图用IfElse和IFNULL扩展从beberlei/DoctrineExtensions bundle,但我无法找到正确的语法。

此查询在CASE WHEN时无效。

感谢您的帮助。

回答

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(); 
+0

我不能再使用NULL测试,因为我改变我的表我的价值观。显然,一个GROUP_CONCAT和一个CASE当它们在同一个查询中不起作用时,我在这里发布了这个问题:https://github.com/beberlei/DoctrineExtensions/issues/222 – Felurian

+0

但是,我应该使用setParameter()在我的查询中设置NULL值:'CASE WHEN tag.picto =:search THEN:replace ELSE tag.picto END' [...]' - > setParameters(array('search'=> NULL,'replace'=> 'NULL'));' – Felurian