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

时间:2015-02-18 14:31:48

标签: sql ruby-on-rails postgresql orm

我收到以下错误:

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') || ' ''' || ':*'))))

1 个答案:

答案 0 :(得分:0)

发现问题了!以下句子:

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

应该是

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

这适用于所有句子:地点,所有者,用户,ID