2017-04-17 104 views
2

這裏是我的SQLSQL在PostgreSQL中/ PHP語法錯誤

$sqltable3= 
'SELECT 
"Publications"."Pub_ID", 
"Questions"."Question" 
FROM "Publications" 
LEFT JOIN "Aspect_Pub_join" ON "Publications"."Pub_ID"="Aspect_Pub_join"."Pub_ID" 
LEFT JOIN "Aspect_question_join" ON "Aspect_Pub_join"."Aspect_ID"="Aspect_question_join"."Aspect_ID" 
LEFT JOIN "Questions" ON "Aspect_question_join"."Question_ID"="Questions"."Question_ID" 
Where "Publications"."Pub_ID"=$1 
GROUP BY "Questions"."Question" 
ORDER BY "Publications"."Pub_ID" ASC'; 

這裏的錯誤:

Warning: pg_query_params(): Query failed: ERROR: column "Publications.Pub_ID" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT "Publications"."Pub_ID", "Questions"."Question" FROM ...^in ...

我使用Publications.Pub_ID的順序由作爲聚合函數,所以我我不知道我哪裏出錯了?相同的查詢在MySQL中運行良好(我知道它不那麼挑剔)。

如果我通過函數拿走組和命令,然後sql運行,但顯然不會返回所需的結果。

回答

1

需要包括羣組中的出版物。

$sqltable3= 
'SELECT 
"Publications"."Pub_ID", 
"Questions"."Question" 
FROM "Publications" 
LEFT JOIN "Aspect_Pub_join" ON "Publications"."Pub_ID"="Aspect_Pub_join"."Pub_ID" 
LEFT JOIN "Aspect_question_join" ON "Aspect_Pub_join"."Aspect_ID"="Aspect_question_join"."Aspect_ID" 
LEFT JOIN "Questions" ON "Aspect_question_join"."Question_ID"="Questions"."Question_ID" 
Where "Publications"."Pub_ID"=$1 
GROUP BY "Questions"."Question", 
"Publications"."Pub_ID" 
ORDER BY "Publications"."Pub_ID" ASC'; 
0

錯誤是相當清楚的,因爲它的要求在選擇列表中的另一列添加到您的group by條款如下圖所示。雖然看不到任何理由爲什麼你需要那group by,因爲你沒有使用任何聚合函數

GROUP BY "Questions"."Question" , "Publications"."Pub_ID"