2013-06-12 97 views
1

我已經在我的問題得到很好的答案herePostgreSQL的,語法查詢問題

所以如果我創建表:

CREATE TABLE IF NOT EXISTS usedfood 
(food_code int, name text, qty int, meas text); 

INSERT INTO usedfood (food_code, name, qty, meas) 
VALUES (10, 'spaghetti', 3, 'pcs'), 
     (156, 'mayonnaise', 2, 'pcs'), 
     (173, 'ketchup', 1, 'pcs'), 
     (172, 'bolognese sauce', 2, 'pcs'), 
     (173, 'ketchup', 1, 'pcs'), 
     (10, 'spaghetti', 2, 'pcs'), 
     (156, 'mayonnaise', 1, 'pcs'); 

CREATE TABLE IF NOT EXISTS ingredients 
(food_code int, ingr_code int, name text, qty decimal(11,3), meas text); 

INSERT INTO ingredients (food_code, ingr_code, name, qty, meas) 
VALUES (10, 1256, 'spaghetti rinf', 75, 'gramm'), 
     (156, 1144, 'salt', 0.3, 'gramm'), 
     (10, 1144, 'salt', 0.5, 'gramm'), 
     (156, 1140, 'fresh egg', 50, 'gramm'), 
     (172, 1138, 'tomato', 80, 'gramm'), 
     (156, 1139, 'mustard', 5, 'gramm'), 
     (172, 1136, 'clove', 1, 'gramm'), 
     (156, 1258, 'oil', 120, 'gramm'), 
     (172, 1135, 'laurel', 0.4, 'gramm'), 
     (10, 1258, 'oil', 0.4, 'gramm'), 
     (172, 1130, 'corned beef', 40, 'gramm'); 

,並從我的PostgreSQL執行這個查詢:

SELECT SUM(f.qty) used_times, 
COALESCE(i.ingr_code, f.food_code) code, 
COALESCE(i.name, f.name) name, 
SUM(COALESCE(i.qty, 1) * f.qty) qty, 
COALESCE(i.meas, f.meas) meas 
FROM usedfood f LEFT JOIN ingredients i 
ON f.food_code = i.food_code 
GROUP BY i.ingr_code, i.name 

...我仍然不能得到像這樣的預期結果SQL fiddle

我得到這樣的錯誤:

ERROR: syntax error at or near "name" LINE 1: ...code, f.food_code) code, COALESCE(i.name, f.name) name, SUM(...

ERROR: column "f.food_code" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...LECT SUM(f.qty) used_times, COALESCE(i.ingr_code, f.food_cod...

ERROR: column "f.name" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...(i.ingr_code, f.food_code) code, COALESCE(i.name, f.name) in...

ERROR: column "i.meas" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...me, SUM(COALESCE(i.qty, 1) * f.qty) qty, COALESCE(i.meas, f....

ERROR: column "f.meas" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...COALESCE(i.qty, 1) * f.qty) qty, COALESCE(i.meas, f.meas) me...

這是最有可能的,這個代碼是不是在PostgreSQL完全兼容,所以請,如果有人可以從查詢解決這個代碼是正確的從PostgreSQL的系統運行。

+1

什麼是不清楚關於「*列」f.meas「必須出現在GROUP BY子句*」 –

+0

這很清楚,但是當我作爲「錯誤報告」建議我得到不正確的結果。 –

+0

「不正確的結果」太模糊。告訴我們你期望的輸出是什麼(根據你的樣品數據) –

回答

3

這些問題主要是由於PostgreSQL(與大多數其他RDBMS類似,但與MySQL不同)需要將選定項目分組或彙總到分組/聚合查詢中。請嘗試:

SELECT SUM(f.qty) used_times, 
     COALESCE(i.ingr_code, max(f.food_code)) code, 
     COALESCE(i.name, max(f.name)) "name", 
     SUM(COALESCE(i.qty, 1) * f.qty) qty, 
     COALESCE(max(i.meas), max(f.meas)) meas 
    FROM usedfood f LEFT JOIN ingredients i 
    ON f.food_code = i.food_code 
GROUP BY i.ingr_code, i.name 

SQLFiddle here

+3

這應該是「*所有基於SQL的數據庫都要求對選定的項目進行分組或聚合,因爲它是以這種方式由SQL標準定義的,MySQL違反了這些要求並簡單地返回隨機結果*」 –

+0

是的,就是這樣。我使用C語言,現在得到正確的結果,沒有錯誤和警告。謝謝。 –

+0

@ user973238:不客氣。 –