簡體   English   中英

0行影響SQL查詢消息

[英]0 rows affected SQL query message

我在eclipse的數據庫中有這個表:

Recipe = (idR, recipeTitle, prepText, cuisineType, mealType)
Ingredient = (idI, ingrDesc)
RecipIngr = (idR*, idI*)

我正在嘗試顯示含有最少成分的食譜的標題。 這是我目前的查詢:

  SELECT recipetitle, COUNT(*)
  FROM recipe
  GROUP BY recipetitle
  HAVING COUNT(recipetitle) =
  (SELECT MAX(COUNT(ingrdesc))
  FROM ingredient
  GROUP BY recipetitle);

這顯示“0行受影響”,所以我不知道我做錯了什么。 有幫助嗎?

您必須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
)

但是,我不是百分之百確定這是否會起作用,因為我不知道你正在使用什么數據庫引擎。

根據您的需要選擇第一個匹配或所有匹配 - 對於MSSQL。

第一:

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

所有:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM