Mysql Group_concat在哪里再次组concat如何管理

时间:2015-01-23 06:53:13

标签: php mysql sql database

当我运行此查询结果时

SELECT 
    GROUP_CONCAT(`recipe_ingredient`.`recipeId`) 
FROM `recipe_ingredient` 
WHERE `recipe_ingredient`.`ingredientId` 
    IN(4,11,33,36,54,117,135,136,178,185,218,312,348,378)
ORDER BY `recipeId` ASC  

但我想从上面查询“IN”值我从其他表获取所以我创建新查询但我没有得到结果

见下面的新查询

SELECT 
    GROUP_CONCAT(`recipe_ingredient`.`recipeId`) 
FROM `recipe_ingredient` 
WHERE `recipe_ingredient`.`ingredientId` 
    IN(
        SELECT 
        GROUP_CONCAT(`ingredient`.`ingredientId` 
        ORDER BY `ingredient`.`ingredientId` ASC ) AS unlinkIng 
        FROM `ingredient` 
        WHERE `ingredient`.`ingredientId` IN(4,178) or    `ingredient`.`linkIngredientPerent` IN(4,178)
)
ORDER BY `recipeId` ASC  

2 个答案:

答案 0 :(得分:1)

从子查询中删除GROUP_CONCAT

SELECT 
    GROUP_CONCAT(`recipe_ingredient`.`recipeId`) 
FROM 
    `recipe_ingredient` 
WHERE 
    `recipe_ingredient`.`ingredientId` IN (SELECT `ingredient`.`ingredientId` AS unlinkIng 
                                           FROM `ingredient` 
                                           WHERE `ingredient`.`ingredientId` IN (4, 178) 
                                              OR `ingredient`.`linkIngredientPerent` IN (4, 178))
ORDER BY 
    `recipeId` ASC  

答案 1 :(得分:0)

您在子查询中获得的结果是什么?请先确认是否返回任何结果。否则查询似乎没问题。

SELECT 
        GROUP_CONCAT(`ingredient`.`ingredientId` 
        ORDER BY `ingredient`.`ingredientId` ASC ) AS unlinkIng 
        FROM `ingredient` 
        WHERE `ingredient`.`ingredientId` IN(4,178) or    `ingredient`.`linkIngredientPerent` IN(4,178)