简体   繁体   English

SQL SELECT语句MAX日期和表连接

[英]SQL SELECT statement MAX date and table joins

In MySQL, I have a two tables as below : 在MySQL中,我有两个表格如下:

         ClientTable               
    clientID    clientName            
    1           Client A              
    2           Client B             
    3           Client C             
    4           Client D             
    5           Client E              
    6           Client F              



                 NotesTable
    noteID  clientID    note    noteDate
    1           3       Test 1    12-Jun-14
    2           3       Test 2    18-Aug-14
    3           4       Test 3    23-Oct-14
    4           6       Test 4    25-May-14
    5           3       Test 5    25-Nov-14
    6           6       Test 6    16-Jul-14

I want to select all the clients from the client table and, where a note exists for the client, the date of the latest note entry. 我想从客户端表中选择所有客户端,并且在客户端存在注释的情况下,选择最新注释条目的日期。 If no note exists for a client, then return null for the noteDate. 如果客户端不存在任何注释,则为noteDate返回null。 Desired result set as follows : 期望的结果集如下:

    client ID   clientName  latestNoteDate  
    1           Client A    null    
    2           Client B    null    
    3           Client C    25-Nov-14   
    4           Client D    23-Oct-14   
    5           Client E    null    
    6           Client F    16-Jul-14   

Any help appreciated, I have tried a few options using nested Select with MAX(noteDate) and various left joins but can't seem to get it right. 任何帮助表示赞赏,我尝试了一些使用嵌套的Select with MAX(noteDate)和各种左连接的选项,但似乎无法正确使用。

Why all the subqueries? 为什么所有子查询?

select ct.clientID, ct.clientName,max(nt.noteDate) latestNoteDate
from ClientTable  ct
left outer join NotesTable nt
on ct.clientID = nt.clientID 
group by ct.clientID, ct.clientName

You can use an outer join with a subquery: 您可以将子outer join与子查询一起使用:

select c.clientid, c.clientname, n.latestnotedate
from client c
   left join (
       select clientId, max(noteDate) latestnotedate
       from notes
       group by clientId
   ) n on c.clientId = n.clientId

This assumes the max(noteDate) is the latest note entry. 这假设max(noteDate)最新的音符条目。 If that's not the case, easy enough to use the noteid instead and then just include one additional join. 如果不是这种情况,那么很容易使用noteid ,然后只包含一个额外的连接。

Looks like a good place for using a sub-query. 看起来像是使用子查询的好地方。 Try something like: 尝试类似的东西:

select c.id, c.name, n.latestNoteDate from client c
left join
(select clientid, MAX(notedate) as latestNoteDate from note
group by clientid) as n on n.clientid = c.id

The key is, find the data you want from the notes table first, then use that to join with the client data later. 关键是,首先从notes表中找到你想要的数据,然后用它来加入客户数据。

Try the following code, which uses a correlated sub-query. 请尝试以下代码,该代码使用相关的子查询。

SELECT ct.clientID,
       ct.clientName,
       (SELECT MAX(noteDate)
        FROM   notesTable nt
        WHERE  nt.clientID = ct.clientId)
FROM   clientTable ct
select clienttable.clientID, clienttable.clientName, notes.noteDate
left outer join NotesTable notes on notes.clientID = clienttable.clientID and noteDate = (select      max(noteDate) from NotesTable where notes.clientID = clienttable.clientID)

It will return null if there are no note entries. 如果没有注释条目,它将返回null。

OR 要么

select clienttable, clientID, clienttable.clientName, (select max(noteDate) from NotesTable where notes.clientID = clienttable.clientID) noteDate

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

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