PostgreSQL在单个子查询列上使用Group By时选择Joined Subquery列

时间:2018-03-04 23:05:00

标签: sql postgresql

这就是我要做的事情,它按标签搜索图像,然后按图像的书签数量排序:

SELECT images_sq.*
FROM
(SELECT images.*
FROM "images"
JOIN image_tags ON images.id = image_tags.image_id
JOIN tags ON tags.id = image_tags.tag_id AND tags.name IN ('cards','clinical')
WHERE "images"."deleted_at" IS NULL
GROUP BY images.id HAVING (COUNT(tags.id) = '2')) images_sq
LEFT JOIN bookmarks
ON images_sq.id = bookmarks.image_id AND bookmarks.deleted_at IS NULL
group by images_sq.id Order By COUNT(bookmarks.id) DESC

但我收到错误:

ERROR:  column "images.created_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT images.*

这有点奇怪,因为以下工作正常(请注意图像子查询与图像表连接):

SELECT images.*
FROM
(SELECT images.*
FROM "images"
JOIN image_tags ON images.id = image_tags.image_id
JOIN tags ON tags.id = image_tags.tag_id AND tags.name IN ('cards','clinical')
WHERE "images"."deleted_at" IS NULL
GROUP BY images.id HAVING (COUNT(tags.id) = '2')) images_sq
JOIN images on images.id = images_sq.id
LEFT JOIN bookmarks
ON images.id = bookmarks.image_id AND bookmarks.deleted_at IS NULL
GROUP BY images.id Order By COUNT(bookmarks.id) DESC;

为什么我不能在第一个示例中选择images子查询中的所有列,就像我在第二个示例中执行images表一样?显然,我做错了什么,误解了什么。我该怎么办呢?

编辑(添加我的缩写表):

                                       Table "public.bookmarks"
   Column   |           Type           | Collation | Nullable |                Default                
------------+--------------------------+-----------+----------+---------------------------------------
 id         | integer                  |           | not null | nextval('bookmarks_id_seq'::regclass)
 deleted_at | timestamp with time zone |           |          | 
 image_id   | integer                  |           |          | 

                                        Table "public.images"
    Column    |           Type           | Collation | Nullable |              Default               
--------------+--------------------------+-----------+----------+------------------------------------
 id           | integer                  |           | not null | nextval('images_id_seq'::regclass)
 deleted_at   | timestamp with time zone |           |          |  

              Table "public.image_tags"
  Column  |  Type   | Collation | Nullable | Default 
----------+---------+-----------+----------+---------
 image_id | integer |           | not null | 
 tag_id   | integer |           | not null | 

                                       Table "public.tags"
   Column   |           Type           | Collation | Nullable |             Default              
------------+--------------------------+-----------+----------+----------------------------------
 id         | integer                  |           | not null | nextval('tags_id_seq'::regclass)
 name       | text                     |           |          | 

1 个答案:

答案 0 :(得分:0)

Postgres允许这样的结构:

select i.*
from images i . . .
group by i.id

严格因为id在表格中声明是唯一的(可能是主键)。

您的子查询不提供其他信息,因此您必须列出子查询中的所有列。