postgresql - “SELECT”处或附近的 PostgreSQL 语法错误
问题描述
我不知道为什么会出现这个错误。查询工作正常,然后我尝试使它更漂亮,然后发生这种情况
UPDATE "topTenCategories" SET "membersCount" = "tempTable"."newVal" FROM
(
VALUES
(
SELECT count(*), "g"."id" FROM
"groups" AS "g"
LEFT JOIN
"groupMembers" As "gm"
ON "g"."id" = "gm"."groupId"
WHERE "g"."isCategory" is true and "g"."parentCategoryId" is null group by ("g"."id")
)
) AS tempTable ("newVal", "id")
WHERE "topTenCategories"."groupId" = "tempTable"."id";
ERROR: syntax error at or near "SELECT"
LINE 5: SELECT count(*), "g"."id" FROM
^
SQL state: 42601
Character: 137
任何帮助将不胜感激
解决方案
您不能将 aSELECT
放入这样的values
子句中。该VALUES
子句用于常量值,此处不需要。
UPDATE "topTenCategories"
SET "membersCount" = tempTable."newVal"
FROM (
SELECT count(*), "g"."id"
FROM "groups" AS "g"
LEFT JOIN "groupMembers" As "gm" ON "g"."id" = "gm"."groupId"
WHERE "g"."isCategory" is true
and "g"."parentCategoryId" is null group by ("g"."id")
) AS tempTable("newVal", "id")
WHERE "topTenCategories"."groupId" = tempTable."id";
您还需要在引用时删除双引号,temptable
因为引号使其区分大小写并且tempTable
名称不同于"tempTable"
一般来说,强烈建议一开始就避免使用那些可怕的带引号的标识符。
推荐阅读
- freertos - FreeRTOS Wifi 关闭事件不调用 vApplicationIPNetworkEventHook
- r - 如何将 MM::SS 字符串转换为时间格式,然后对其进行操作
- mysql - 在 mysql 工作台中,我正在尝试为我的 bike_store 表创建一个简单的函数。查询如下,请提出解决方案
- javascript - 一次又一次地改变一个 div 内容而不丢失以前的 html
- r - R 的 MICE 库中用于估算缺失数据的错误
- ios - Lastpass iOS 自动填充过滤器
- ios - 在 Xcode UI 测试中,我在哪里可以找到我的应用程序的 xcresult 日志?
- android - 公共类 PeripheralManager - 返回所有存根
- excel-2010 - 当从 SQL 数据库编辑(删除)Power Query 中的表列时,此编辑是否永久?
- python - 将 JSON 文件(或 XML,如果更简单的话)转换为 Python 中修改后的 XML 文件