2013-08-03 122 views
1

這個查詢給我錯誤,所以這個查詢有什麼問題?這個SQL查詢有什麼問題

SELECT recipes.recipeId as recipeId, 
     recipes.title as title, 
     recipes.cookingtime as cookingtime, 
     recipes.serving as serving, 
     schedule.catId as catId, 
     images.imagePath AS imagePath 
    FROM recipes 
INNER JOIN schedule ON recipes.recipeId = schedule.recipeId 
INNER JOIN images ON images.recipeId = recipes.recipeId 
GROUP BY recipeId 
WHERE schedule.day = 'saturday' 
ORDER BY catId ASC 

我認爲在查詢中group by的位置不正確。

+2

雖然你能猜出你的答案(並有它由echo_Me確認),你應該總是包含錯誤信息你的問題。 –

回答

1

你想正確的。 GROUP BY應在WHERE條款和ORDER BY條款之後。

SELECT recipes.recipeId as recipeId, 
     recipes.title as title, 
     recipes.cookingtime as cookingtime, 
     recipes.serving as serving, 
     schedule.catId as catId, 
     images.imagePath AS imagePath 
    FROM recipes 
INNER JOIN schedule ON recipes.recipeId = schedule.recipeId 
INNER JOIN images ON images.recipeId = recipes.recipeId 
WHERE schedule.day = 'saturday' 
GROUP BY recipeId 
ORDER BY catId ASC 
+1

謝謝了:) –

+0

@AdnanAhmed - 很高興幫助你。 :) – hims056

1

應該是這樣的,在那裏再由,組,然後訂購

 WHERE schedule.day = 'saturday' 
    GROUP BY recipeId 

    ORDER BY catId ASC 
1

這裏沒有聚合功能,你不能使用group by。 和group by條件在where條款之後和order by條款之前。

SELECT recipes.recipeId as recipeId, 
      recipes.title as title, 
      recipes.cookingtime as cookingtime, 
      recipes.serving as serving, 
      schedule.catId as catId, 
      images.imagePath AS imagePath 

    FROM 
      recipes INNER JOIN schedule 
           ON recipes.recipeId = schedule.recipeId 

             INNER JOIN images 
                ON images.recipeId = recipes.recipeId 

-- GROUP BY recipeId 

    WHERE schedule.day = 'saturday' ORDER BY catId ASC 
0

只需更換的

GROUP BY recipeId 

訂單後

WHERE schedule.day = 'saturday' 

它必須..

SELECT recipes.recipeId as recipeId, 
     recipes.title as title, 
     recipes.cookingtime as cookingtime, 
     recipes.serving as serving, 
     schedule.catId as catId, 
     images.imagePath AS imagePath 
FROM recipes 
INNER JOIN schedule ON recipes.recipeId = schedule.recipeId 
INNER JOIN images ON images.recipeId = recipes.recipeId 
WHERE schedule.day = 'saturday' 
GROUP BY recipeId 
ORDER BY catId ASC 
+0

爲投票回答此問題的用戶。請清除投票的理由.... – Vijay