2009-07-03 161 views
3

我正在查找正確的SQL代碼來連接兩個表並僅顯示詳細信息表的最後一條記錄。連接表的最後一條記錄

我有2個表的數據塊,

Deals 
    DealID 
    Dealname 
    DealDetails 

DealComments 
    dcID 
    DealID 
    CommentTime 
    CommentPerson 
    Comment 

有每筆交易多份意見,但我想創建一個顯示所有優惠只在最後一評論的來自每一筆交易視圖(由CommentTime)字段確定

+0

你想要什麼,如果對於一些交易有兩個或更多最後的評論與完全相同的時間? – 2009-07-04 23:22:58

回答

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) 

編輯:我沒看過最初的問題足夠接近,並沒有注意到,都需要在查看所有優惠券行。下面是我的修訂答案:

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) 
+0

這個效果很好,但它只返回有評論的交易。我還想看到沒有評論的交易 – 2009-07-04 20:27:27

+0

@Khalid - 我編輯了我的答案,新的SQL應返回所有交易行。 – 2009-07-04 23:08:33

+0

優秀,正是我所需要的。感謝John – 2009-07-05 05:24:17

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 
1

不是很優雅,但在甲骨文的工作原理:

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 
+0

爲什麼你將其描述爲「不太優雅」? – 2009-11-03 15:15:47

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) 
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) 
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當存在比任何給定的時間註釋爲特定DealID大不CommentTime

編輯:Alex Kuznetsov在評論中敏銳地指出:OP要求顯示所有的交易 - 交易是否有評論與否。所以我把第一個JOININNER更改爲LEFT OUTER

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) 
相關問題