获取最新的日期记录

时间:2014-05-14 12:02:27

标签: sql date

我的SQL数据库中有两个表。

ID  CDATE  
1   12/04/14  
2   14/05/14  
3   20/01/14  


ID  NAME  
1    A  
2    A  
3    B

但我需要输出如下。

NAME  CDATE  
A     14/05/14  
B     20/01/14

你对我有什么建议吗?

4 个答案:

答案 0 :(得分:1)

SELECT MAX(t1.CDATE), t2.NAME FROM table1 t1 LEFT JOIN table2 t2 ON t1.ID = t2.ID GROUP BY t2.NAME ORDER BY t1.CDATE DESC

答案 1 :(得分:0)

试试这个:

SELECT t1.cdate, t2.name
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id=t2.id
ORDER BY t1.cdate DESC;

编辑:

SELECT s1.cdate,
  t2.name
FROM table2 t2
LEFT JOIN
  (SELECT ID, MAX(cdate) cdate FROM table1 GROUP BY ID
  ) s1
ON s1.id=t2.id
ORDER BY s1.cdate DESC;

答案 2 :(得分:-1)

编辑:因为你改变了问题......

SELECT TOP 2 n.NAME, d, CDATE
FROM Dates d
LEFT JOIN Names n ON d.ID=n.ID
ORDER BY d.CDATE;

答案 3 :(得分:-1)

要创建表,您需要在此表之间创建关系

http://www.w3schools.com/sql/sql_foreignkey.asp

然后从两个表中获取数据 http://www.w3schools.com/sql/sql_join.asp