0

使用 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

是否有可能只有第一个结果?我只想按项目获得一个结果。

谢谢!

4

1 回答 1

1

你可以写:

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
;
于 2012-11-24T16:11:49.737 回答