连接多个表以获得帖子评论

时间:2019-04-10 16:13:26

标签: sql postgresql

我有下表:

CREATE TABLE public.post (
    id varchar NOT NULL,
    user_id varchar NOT NULL,
    "content" text NOT NULL,
    location_details varchar NOT NULL,
    datetime timestamptz NOT NULL,
    "blocked" bool NOT NULL DEFAULT false,
    anonymous bool NOT NULL DEFAULT false,
    CONSTRAINT post_pk PRIMARY KEY (id),
    CONSTRAINT post1_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE TABLE public.post_comments (
    id varchar NOT NULL,
    post_id varchar NOT NULL,
    "content" text NOT NULL,
    date_time timestamp NOT NULL,
    user_id varchar NOT NULL,
    CONSTRAINT post_comments_id_post_id_key UNIQUE (id, post_id),
    CONSTRAINT post_comments_post_id_fkey FOREIGN KEY (post_id) REFERENCES post(id),
    CONSTRAINT post_comments_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE TABLE public.post_photos (
    post_id varchar NOT NULL,
    id varchar NOT NULL,
    date_time timestamp NOT NULL,
    title varchar NULL,
    description text NULL,
    "path" varchar NOT NULL,
    CONSTRAINT post_photos_pk PRIMARY KEY (id),
    CONSTRAINT post_photos_post_id_fkey FOREIGN KEY (post_id) REFERENCES post(id)
);

CREATE TABLE public.tag_post (
    tag_id varchar NOT NULL,
    post_id varchar NOT NULL,
    CONSTRAINT tag_post_tag_id_post_id_key UNIQUE (tag_id, post_id),
    CONSTRAINT tag_post_post_id_fkey FOREIGN KEY (post_id) REFERENCES post(id),
    CONSTRAINT tag_post_tag_id_fkey FOREIGN KEY (tag_id) REFERENCES tags(id)
);

CREATE TABLE public.tags (
    id varchar NOT NULL,
    description text NULL,
    CONSTRAINT tags_pk PRIMARY KEY (id)
);

我必须在PostgreSQL中加入这些表,我正在执行以下查询:

select distinct p.id, pc.id, p."content",  p.anonymous, p.datetime, p.location_details, p.user_id, ph.id, ph."path", ph.title, ph.date_time, tp.tag_id from post p 
    left JOIN post_comments pc on p.id=pc.post_id
    left JOIN post_photos ph on p.id=ph.post_id 
    left JOIN tag_post tp on p.id=tp.post_id
    where p.id='863550630468626253'
    group by p.id, pc.id

我收到此错误

SQL Error [42803]: ERROR: column "ph.id" must appear in the GROUP BY clause or be used in an aggregate function
  Position: 242

基本上,该查询应该获得一个帖子,它是最新的评论,标签,帖子的照片。这是表格的数据:

post

id                |user_id            |content         |location_details|datetime           |blocked|anonymous|
------------------|-------------------|----------------|----------------|-------------------|-------|---------|
863550630468626253|1665058285911882257|Hi hello jwje 23|Mumbai     |2019-04-07 00:08:11|false  |false    |

tag_post

tag_id  post_id
java    863550630468626253
tech    863550630468626253

post_comments

id                  |post_id           |content                                  |date_time          |user_id            |
--------------------|------------------|-----------------------------------------|-------------------|-------------------|
1727639960713835685 |863550630468626253|hello comment1!<b>ss</b>                 |2019-04-09 19:08:55|1665058285911882257|
-7642691544064884314|863550630468626253|hello comment12mm3!<b>ss</b>             |2019-04-09 20:18:17|1665058285911882257|
579985859487432237  |863550630468626253|hello hjjh  jhjhjh comment12mm3!<b>ss</b>|2019-04-09 20:22:45|1665058285911882257|
-2764275200080807769|863550630468626253|hello hjjh  jhjhjh comment12mm3!<b>ss</b>|2019-04-09 20:40:28|1665058285911882257|

post_photos

post_id|id|date_time|title|description|path|
-------|--|---------|-----|-----------|----|

1 个答案:

答案 0 :(得分:0)

您还没有使用任何聚合函数,因此我不需要按组进行分组,我认为您需要row_number()

with cte as
(select  p.id, pc.id, p."content",
  p.anonymous, p.datetime, 
p.location_details, p.user_id, ph.id, ph."path",
 ph.title, ph.date_time, tp.tag_id 
 ,row_number()over(partition by  p.id order by datetime desc) rn
   from post p 
    left JOIN post_comments pc on p.id=pc.post_id
    left JOIN post_photos ph on p.id=ph.post_id 
    left JOIN tag_post tp on p.id=tp.post_id
    where p.id='863550630468626253'
) select * from cte where rn=1