Postgresql:FROM 中的子查询必须有别名 - 具有多个连接

Posted

技术标签:

【中文标题】Postgresql:FROM 中的子查询必须有别名 - 具有多个连接【英文标题】:Postgresql: Subquery in FROM must have an alias - with multiple joins 【发布时间】:2015-02-18 14:31:47 【问题描述】:

我收到以下错误:

ERROR:  subquery in FROM must have an alias
LINE 11:   (SELECT "domiciles"."id" AS id,
           ^
HINT:  For example, FROM (SELECT ...) [AS] foo.

使用以下 SQL 查询:

SELECT "domiciles".*
FROM "domiciles"
LEFT OUTER JOIN
  (SELECT "domiciles"."id" AS id,
          string_agg("locations"."name"::text, ' ') AS name
   FROM "domiciles"
   INNER JOIN "locations" ON "locations"."id" = "domiciles"."place_id"
   AND "locations"."type" IN ('Place')
   GROUP BY "domiciles"."id") place ON place.id = "domiciles"."id"
LEFT OUTER JOIN
  (SELECT "domiciles"."id" AS id,
          string_agg("accounts"."email"::text, ' ') AS email
   FROM "domiciles"
   INNER JOIN "accounts" ON "accounts"."id" = "domiciles"."user_id"
   AND "accounts"."type" IN ('User')
   GROUP BY "domiciles"."id") user ON user.id = "domiciles"."id"
WHERE "domiciles"."deleted_at" IS NULL

我已尝试在查询的许多部分添加 AS 'some_text',但无法解决此问题。有什么想法吗?

这里是完整的 SQL 查询,以了解我想要做什么:

SELECT "domiciles".*,
       ((ts_rank((to_tsvector('german', unaccent(coalesce("domiciles"."is_default"::text, ''))) || to_tsvector('german', unaccent(coalesce("domiciles"."created_at"::text, ''))) || to_tsvector('german', unaccent(coalesce("domiciles"."updated_at"::text, ''))) || to_tsvector('german', unaccent(coalesce(place.name::text, ''))) || to_tsvector('german', unaccent(coalesce(user.email::text, ''))) || to_tsvector('german', unaccent(coalesce(owner.email::text, '')))), (to_tsquery('german', ''' ' || unaccent('abc') || ' ''' || ':*')), 0))) AS pg_search_rank
FROM "domiciles"
LEFT OUTER JOIN
  (SELECT "domiciles"."id" AS id,
          string_agg("locations"."name"::text, ' ') AS name
   FROM "domiciles"
   INNER JOIN "locations" ON "locations"."id" = "domiciles"."place_id"
   AND "locations"."type" IN ('Place')
   AND "locations"."deleted_at" IS NULL
   GROUP BY "domiciles"."id") place ON place.id = "domiciles"."id"
LEFT OUTER JOIN
  (SELECT "domiciles"."id" AS id,
          string_agg("accounts"."email"::text, ' ') AS email
   FROM "domiciles"
   INNER JOIN "accounts" ON "accounts"."id" = "domiciles"."user_id"
   AND "accounts"."type" IN ('User')
   AND "accounts"."deleted_at" IS NULL
   GROUP BY "domiciles"."id") user ON user.id = "domiciles"."id"
LEFT OUTER JOIN
  (SELECT "domiciles"."id" AS id,
          string_agg("accounts"."email"::text, ' ') AS email
   FROM "domiciles"
   INNER JOIN "locations" ON "locations"."id" = "domiciles"."place_id"
   AND "locations"."type" IN ('Place')
   AND "locations"."deleted_at" IS NULL
   INNER JOIN "accounts" ON "accounts"."id" = "locations"."user_id"
   AND "accounts"."type" IN ('User')
   AND "accounts"."deleted_at" IS NULL
   GROUP BY "domiciles"."id") owner ON owner.id = "domiciles"."id"
WHERE "domiciles"."deleted_at" IS NULL
  AND "domiciles"."user_id" = $1
  AND (((to_tsvector('german', unaccent(coalesce("domiciles"."is_default"::text, ''))) || to_tsvector('german', unaccent(coalesce("domiciles"."created_at"::text, ''))) || to_tsvector('german', unaccent(coalesce("domiciles"."updated_at"::text, ''))) || to_tsvector('german', unaccent(coalesce(place.name::text, ''))) || to_tsvector('german', unaccent(coalesce(user.email::text, ''))) || to_tsvector('german', unaccent(coalesce(owner.email::text, '')))) @@ (to_tsquery('german', ''' ' || unaccent('abc') || ' ''' || ':*'))))

【问题讨论】:

GROUP BY "domiciles"."id") user ON user.id = "domiciles"."id" "user" 是保留字。不要使用它(这里)只需将其重命名为“uuu”或其他东西(这里) 谢谢@wildplasser!使用GROUP BY "domiciles"."id") "user" ON "user"."id" = "domiciles"."id" 可以解决问题。看我的回答。 【参考方案1】:

发现问题了!以下句子

place ON place.id
coalesce(place.name::text, '')

应该是

"place" ON "place"."id"
coalesce("place"."name"::text, '')

这适用于所有句子:place, owner, user, id

【讨论】:

为什么你认为你需要双引号?小写是 PostgreSQL 中对象的默认值,不需要引号。 尽管我得到错误,但引号都可以正常工作。此外,有些词是保留的,不能使用。带引号我可以使用它。请阅读问题中@wildplasser 的评论。

以上是关于Postgresql:FROM 中的子查询必须有别名 - 具有多个连接的主要内容,如果未能解决你的问题,请参考以下文章

MySQL的子查询中FROM和EXISTS子句的使用教程

如何在 Laravel 8 中使用 PostgreSQL 中的子查询通过 group by 子句获取行值?

FROM 中的子查询在 Oracle SQL 中不起作用

使用 from 中的子查询优化 SQL

FROM中的子查询不在Oracle SQL中工作

使用 from 子句中的子查询动态选择表名