gpt4 book ai didi

php - 给定两个 SELECT 语句,如何合并它们以获得一个结果减去另一个结果?

转载 作者:行者123 更新时间:2023-11-29 12:27:23 26 4
gpt4 key购买 nike

我想从 wantedIDs 中删除 UNwantedIDs,如何进行子查询并得到一个结果?

这是我的 UNwantedIDs 查询:

SELECT 
GROUP_CONCAT(DISTINCT `recipe_ingredient`.`recipeId` ORDER BY `recipe_ingredient`.`recipeId` ASC) UNwantedIDs
FROM `recipe_ingredient`
WHERE `recipe_ingredient`.`ingredientId`
IN(SELECT `ingredient`.`ingredientId` AS unlinkIng
FROM `ingredient`
WHERE `ingredient`.`ingredientId` IN(1) or `ingredient`.`linkIngredientPerent` IN(1))
ORDER BY `recipeId` ASC

http://sqlfiddle.com/#!2/84f00/5

这是我的 wantedIDs 查询:

SELECT 
GROUP_CONCAT(DISTINCT `recipe_ingredient`.`recipeId` ORDER BY `recipe_ingredient`.`recipeId` ASC) wantedIDs
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

http://sqlfiddle.com/#!2/84f00/4

结果 ID 例如:

2,3,7,8,11,21,24,36,37,41,67,70,75,80,83,99,108 

最佳答案

我有这个:

SELECT 
GROUP_CONCAT(
DISTINCT `recipe_ingredient`.`recipeId`
ORDER BY `recipe_ingredient`.`recipeId` ASC
) wantedIDs
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)
)
)
AND `recipe_ingredient`.`recipeId` NOT IN (
SELECT
`recipe_ingredient`.`recipeId`
FROM `recipe_ingredient`
WHERE `recipe_ingredient`.`ingredientId`
IN (SELECT `ingredient`.`ingredientId` AS unlinkIng
FROM `ingredient`
WHERE `ingredient`.`ingredientId` IN(1) OR
`ingredient`.`linkIngredientPerent` IN(1)
)
)
ORDER BY `recipeId` ASC

这是用于合并两个查询的 NOT IN 的结果。目前速度相当慢,可能是因为您需要添加一些索引。

http://sqlfiddle.com/#!2/84f00/16

关于php - 给定两个 SELECT 语句,如何合并它们以获得一个结果减去另一个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28106888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com