Sql 如何使用联接获得单个结果?

Sql 如何使用联接获得单个结果?,sql,postgresql,Sql,Postgresql,使用postgres,我编写了以下SQL请求: SELECT projects.id , projects.title , comments.message as comment_message FROM "projects" RIGHT OUTER JOIN comments ON comments.project_id = projects.id GROUP BY projects.id, comments.message 我有这样的结果: id | t

使用postgres,我编写了以下SQL请求:

SELECT projects.id
     , projects.title
     , comments.message as comment_message 
FROM "projects" 
RIGHT OUTER JOIN comments 
ON comments.project_id = projects.id
GROUP BY projects.id, comments.message
我有这样的结果:

 id |     title      | comment_message 
----+----------------+-----------------
  6 | simple project | simple comment
  6 | simple project | simple message
有可能只有第一个结果吗?我只想有一个项目的结果

谢谢

你可以写:

SELECT projects.id,
       projects.title,
       MIN(comments.message) AS comment_message 
  FROM "projects"
 RIGHT
 OUTER
  JOIN comments
    ON comments.project_id = projects.id
 GROUP
    BY projects.id
;

你想要哪个结果?其他评论呢?是的,这是可能的,您必须根据您的需要使用评论消息字段进行过滤。我猜不出第一个结果在哪方面比第二个结果更受欢迎。