日期:2014-05-17  浏览次数:20689 次

sqlserver 多表按照时间排序
现在有A表,B表,两表中都有时间字段,datetime.现在需要按照时间先后来取两表中的数据,请问语句应该怎么写........

求大牛指导!
------最佳解决方案--------------------
先把所有表中的时间字段取出,union,order by, 然后取你需要的部分,比如top 100, top 10 percent啥的,找到,max(datetime)或者min(datetime),然后再在每个表中读取所有符合这个时间条件的数据行就行.
------其他解决方案--------------------
select * from (select datetime字段 from a
union all
select datetime字段 from b) c
order by datetime字段 
------其他解决方案--------------------
SELECT  *
 FROM    a
         INNER JOIN b ON a.主键 = b.主键
 ORDER BY CASE WHEN a.datetime >= b.datetime THEN a.datetime
               ELSE b.datetime
          END
 
------其他解决方案--------------------
select * from a order by [datetime]
select * from b order by [datetime]
------其他解决方案--------------------
引用:
先把所有表中的时间字段取出,union,order by, 然后取你需要的部分,比如top 100, top 10 percent啥的,找到,max(datetime)或者min(datetime),然后再在每个表中读取所有符合这个时间条件的数据行就行.
其实这是个按照时间的分页,并且两表的列数不一样.......

该怎么办啊
------其他解决方案--------------------
引用:
select * from (select datetime字段 from a
union all
select datetime字段 from b) c
order by datetime字段

两表的列数不一样,除了datetime没有相同字段.
------其他解决方案--------------------
引用:
SQL code?123456SELECT  * FROM    a         INNER JOIN b ON a.主键 = b.主键 ORDER BY CASE WHEN a.datetime >= b.datetime THEN a.datetime               ELSE b.datetime          END


你这个变成邻接表了,我要的是两表中的数据依时间排序,每条是分开的 两表没有关联.