Join表的最后记录

时间:2009-07-03 07:57:59

标签: sql

我正在寻找正确的SQL代码来连接2个表并仅显示详细信息表的最后一条记录。

我有一个包含2个表的数据库,

Deals 
   DealID
   Dealname
   DealDetails

DealComments
   dcID
   DealID
   CommentTime
   CommentPerson
   Comment

每笔交易都有多条评论,但我想创建一个显示所有交易的VIEW,并且只显示每笔交易的最后一条评论(由评价时间确定)字段

7 个答案:

答案 0 :(得分:9)

select a.dealid
, a.dealname
, a.dealdetails
, b.dcid
, b.commenttime
, b.commentperson
, b.comment
from deals a, dealcomments b
where b.dealid = a.dealid
  and b.commenttime = (select max(x.commenttime)
                       from dealcomments x
                       where x.dealid = b.dealid)
编辑:我没有仔细阅读初始问题,也没有注意到视图中需要所有DEALS行。以下是我修改后的答案:

select a.dealid
, a.dealname
, a.dealdetails
, b.dcid
, b.commenttime
, b.commentperson
, b.comment
from deals a left outer join (select x.dcid
, x.dealid
, x.commenttime
, x.commentperson
, x.comment
from dealcomments x
where x.commenttime = (select max(x1.commenttime)
                       from dealcomments x1
                       where x1.dealid = x.dealid)) b
on (a.dealid = b.dealid)

答案 1 :(得分:1)

试试这个..

SELECT D.*,DC1.Comment 
FROM Deals AS D
   INNER JOIN DealComments AS DC1
        ON D.DealId = DC1.DealID
    INNER JOIN 
    (
        SELECT 
            DealID,
            MAX(CommentTime) AS CommentTime
        FROM DealComments AS DC2
        GROUP BY DealID
    ) AS DC2
        ON DC2.DealId = DC.DealId
            AND DC2.CommentTime = DC1.CommentTime

答案 2 :(得分:1)

不是很优雅,但适用于Oracle:

select dealid,
       dealname,
       dealdetails,
       comment,
from
(
  select a.dealid,
         a.dealname,
         a.dealdetails,
         b.commenttime,
         b.comment,
         max(commenttime) over (partition by a.dealid) as maxCommentTime
  from deals a inner join dealcomments b on b.dealid = a.dealid
)
where comment = maxCommentTime

答案 3 :(得分:1)

试试这个:

CREATE VIEW DealsWithLastComment
AS
   SELECT
       D.*, DC.*
   FROM
       Deals D INNER JOIN DealComments DC
       ON D.DealID = DC.DealID
       GROUP BY D.DealID, DC.CommentTime
       HAVING DC.CommentTime = MAX(DC.CommentTime)

答案 4 :(得分:1)

select
  d.DealID, dc1.dcID, dc1.Comment, dc1.CommentPerson, dc1.CommentTime
from
  Deals d
inner join
  DealComments dc1 on dc1.DealID = d.DealID
where
  dc1.CommentTime = (select max(dc2.CommentTime) from DealsComments dc2 where dc2.DealId = dc1.DealId)

答案 5 :(得分:1)

强制性无子查询 - 无处答案:

select d.*
       , dc.*
from   Deals d
       left outer join DealComments dc 
       on d.DealID = dc.DealID
       left outer join DealComments dc1 
       on d.DealID = dc1.DealID 
   and 
       dc1.CommentTime > dc.CommentTime
where  dc1.CommentTime is null

DealsDealComments中没有CommentTime超过特定DealID的任何给定评论时间时,向我显示所有内容。

编辑:正如Alex Kuznetsov在评论中敏锐地指出:OP要求显示所有交易 - 交易是否有评论。所以我已将第一个JOININNER更改为LEFT OUTER

答案 6 :(得分:1)

这个查询怎么样。

select * from Deals 
left join DealComments on Deals.DealID = DealComments.DealID and DealComments.CommentTime = (SELECT CommentTime  FROM DealComments WHERE DealComments.DealID = Deals.DealID ORDER BY CommentTime DESC limit 1)