我正在处理相关的食谱查询。当前,它返回包含成分ID的行
SELECT recipeid,GROUP_CONCAT(ingredientid ORDER BY ingredientid ASC) as 'ingredientids' FROM `recipeingredients` GROUP BY recipeid
recipeid ingredientids
1 122,288,364,450,560...
2 122,290,321,450,602...
.
.
我想从上述查询中找到与60%的成分标识或6个或更多给定值(例如122,290,350)匹配的食谱
可以在单个查询中完成吗?
答案 0 :(得分:0)
SELECT recipeid,
GROUP_CONCAT(ingredientid ORDER BY ingredientid ASC) as 'ingredientids'
FROM `recipeingredients`
GROUP BY recipeid
HAVING SUM(ingredientid IN (122, 290, 350)) >= FLOOR(COUNT(*) * 0.6)
OR SUM(ingredientid IN (122, 290, 350)) >= 6
ingredientid IN (122, 290, 350)
函数中的这个SUM()
是一个布尔表达式,返回1表示true,0表示false。