简体   繁体   English

Join表的最后记录

[英]Last record of Join table

I am lookign for the correct SQL code to join 2 tables and show only the last record of the details table. 我正在寻找正确的SQL代码来连接2个表并仅显示详细信息表的最后一条记录。

I have a DB with 2 tables, 我有一张带2张桌子的数据库,

Deals 
   DealID
   Dealname
   DealDetails

DealComments
   dcID
   DealID
   CommentTime
   CommentPerson
   Comment

There are multiple comments for each Deal, but i would like to create a VIEW that displays all of the Deals and only the last Comment from each Deal (determined by the CommentTime) field 每笔交易都有多条评论,但我想创建一个显示所有交易的VIEW,并且只显示每笔交易的最后一条评论(由CommentTime确定)字段

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)

EDIT: I didn't read the initial question close enough and didn't notice that all DEALS rows were needed in the view. 编辑:我没有仔细阅读初始问题,并没有注意到视图中需要所有DEALS行。 Below is my revised answer: 以下是我修改的答案:

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)

Try this.. 尝试这个..

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

Not very elegant, but works in Oracle : 不是很优雅,但在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

Try this: 尝试这个:

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

Obligatory no-subquery-nowhere-answer: 强制性的无子查询 - 无处答案:

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

Show me everything in Deals and DealComments when there exists no CommentTime greater than any given comment time for a particular DealID . 给我看在一切DealsDealComments当不存在CommentTime比任何给定的时间注释为特定更大DealID

Edit: as Alex Kuznetsov astutely points out in a comment: the OP requested that all deals be displayed -- whether a deal has a comment or not. 编辑:正如Alex Kuznetsov在评论中敏锐地指出:OP要求显示所有交易 - 交易是否有评论。 So I have changed the first JOIN from INNER to LEFT OUTER . 所以我已经将第一个JOININNER更改为LEFT OUTER

What about this query. 这个查询怎么样?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM