0
\ d評論PostgreSQL的SQL語句:無法獲取所需的最新/最近的評論,需要顯示了最新評論
Table "public.comments"
Column | Type | Modifiers
------------+------------------------+-------------------------------------------------------
id | integer | not null default nextval('comments_id_seq'::regclass)
post_id | integer | not null
name | character varying(255) | not null
email | character varying(255) | not null
content | character varying(500) | not null
created_at | date |
updated_at | date |
Indexes:
"comments_pkey" PRIMARY KEY, btree (id)
對於這一說法
select post_id,created_at from comments order by created_at limit 5;
獨特posts.title我
post_id | created_at
---------+------------
5 | 2011-07-11
5 | 2011-07-11
5 | 2011-07-11
8 | 2011-07-11
2 | 2011-07-17
(5 rows)
但我需要這樣
結果0post_id | created_at
---------+------------
5 | 2011-07-11
8 | 2011-07-11
2 | 2011-07-17
(3 rows)
我該如何重寫sql語句來獲得這三行作爲結果?
\ d柱
Table "public.posts"
Column | Type | Modifiers
-------------+------------------------+----------------------------------------------------
id | integer | not null default nextval('posts_id_seq'::regclass)
title | character varying(100) | not null
content | character varying(500) | not null
created_at | date |
updated_at | date |
tags | character varying(55) | not null default '50'::character varying
category_id | integer | not null default 1
Indexes:
"posts_pkey" PRIMARY KEY, btree (id)
與來自三行我需要從職位表中的posts.title三個ID。 我如何編寫sql語句來獲取post.titt與comments.post_id = 5或8或2?
我需要得到那些不同的posts.title最新評論。 – shibly
@guru:我已經更新了答案。 –
你爲什麼使用MAX函數?我需要最新comments.id的結果,這意味着通過created_at創建了comments.id命令。然後我需要內部加入posts.id來獲得posts.title。 – shibly