日期:2014-05-19  浏览次数:20422 次

求一段多表联查的SQL语句
求一条SQL语句,有两个表:
[TableA]:
AID   AName   DateA
1       aaa1     2007-3-1
2       aaa2     2007-3-2
3       aaa3     2007-3-3
-------------------
[TableB]:
BID   Bname   DateB
12       www1     2007-3-1
13       www2     2007-3-2
14       www3     2007-3-2
==================
如何查询出这样的格式:
Date               ID               Name
2007-3-1       1                 aaa1
2007-3-1       12               www1
2007-3-2       2                 aaa2
2007-3-2       13               www2
2007-3-2       14               www3
2007-3-3       3                 aaa3
===================
两个表唯一的对应关系就是日期DATE,请问如何写呢?

------解决方案--------------------
select Date,ID,Name from
(
select * from TableA
union
select * from TableB
order by Date DESC
)as U
这样应该可以了吧
------解决方案--------------------
select convert(char(10),DateA,126) as Date,AName as Name,AID as ID from A
union
select convert(char(10),DateB,126) as Date,BName as Name,BID as ID from B

order by Date