gpt4 book ai didi

php - postgresql/php 中的 SQL 语法错误

转载 作者:行者123 更新时间:2023-11-29 12:17:48 25 4
gpt4 key购买 nike

这是我的sql

$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 ...

我在 order by 中使用 Publications.Pub_ID 作为聚合函数,所以我不确定哪里出错了?同样的查询在 mysql 中运行良好(我知道它不那么挑剔)。

如果我取消 group by 和 order by 函数,则 sql 会运行,但显然不会返回所需的结果。

最佳答案

需要在分组依据中包含 Publications。

$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';

关于php - postgresql/php 中的 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43460338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com