I have two tables in my SQL Database.

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


ID  NAME  
1    A  
2    A  
3    B

But I need the output as below.

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

Do you have any suggestions for me ?

有帮助吗?

解决方案 2

Try this:

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

Edit:

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;

其他提示

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

EDIT: Since you changed the question...

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

To Make table you need to create relation between this table

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

and then to get data from two table http://www.w3schools.com/sql/sql_join.asp

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top