[英]0 rows affected SQL query message
I have this table in my database in eclipse: 我在eclipse的数据库中有这个表:
Recipe = (idR, recipeTitle, prepText, cuisineType, mealType)
Ingredient = (idI, ingrDesc)
RecipIngr = (idR*, idI*)
and I'm trying to display the title of the Recipe containing the fewest ingredients. 我正在尝试显示含有最少成分的食谱的标题。 Here is my query so far: 这是我目前的查询:
SELECT recipetitle, COUNT(*)
FROM recipe
GROUP BY recipetitle
HAVING COUNT(recipetitle) =
(SELECT MAX(COUNT(ingrdesc))
FROM ingredient
GROUP BY recipetitle);
This shows "0 rows affected" so I have no idea what i'm doing wrong. 这显示“0行受影响”,所以我不知道我做错了什么。 Any help? 有帮助吗?
You have to JOIN
your tables to count number of ingredients for every recipe: 您必须JOIN
表格以计算每种食谱的成分数量:
SELECT
r.recipeTitle,
COUNT(*) AS nbOfIng
FROM
recipe r
JOIN
RecipIngr ri ON r.idR = ri.idR
GROUP BY
r.idR, r.recipeTitle
HAVING
nbOfIng = (
SELECT
MAX(COUNT(*))
FROM
recipe r2
JOIN
RecipIngr ri2 ON r2.idR = ri2.idR
GROUP BY
r2.idR
)
However, I'm not 100% sure if that's gonna work, because I don't know what database engine you're using. 但是,我不是百分之百确定这是否会起作用,因为我不知道你正在使用什么数据库引擎。
Either to select the first match, or all matches, depending on what you need - for MSSQL. 根据您的需要选择第一个匹配或所有匹配 - 对于MSSQL。
First: 第一:
SELECT TOP 1 RecipeTitle = R.recipeTitle
,IngredientCount = COUNT(*)
FROM Recipe R
INNER JOIN RecipIngr RI ON R.idR = RI.idR
INNER JOIN Ingredient I ON RI.idI = I.idI
GROUP BY R.recipeTitle
All: 所有:
SELECT dtbl.RecipeTitle
FROM (SELECT RecipeTitle = R.recipeTitle
,IngredientCount = COUNT(*)
,rank = rank() OVER (Partition BY R.recipeTitle ORDER BY COUNT(*))
FROM Recipe R
INNER JOIN RecipIngr RI ON R.idR = RI.idR
INNER JOIN Ingredient I ON RI.idI = I.idI
GROUP BY R.recipeTitle) dtbl
WHERE dtbl.rank = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.