![](/img/trans.png)
[英]How to get data from two API calls, merge them and display them in a table with my other data?
[英]Given two SELECT statements, how to merge them to get one result minus the other?
我想從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
的結果。 目前速度很慢,可能是因為您需要添加一些索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.