我用的是MY-SQL数据库。a b 两表 没有任何关系 现在需要联合两表,select后的结果集得到这样的显示效果    结果集有ID,TYPE,DATE这3个字段 ID就是A 或B表原有数据的ID值,type只为0或者1 0就是来自A表的数据 1就是来自B表的数据。结果集需要根据时间的降序排列。A 表数据类似
ID Title content date
1  aaa    aaa    2008-9-5
2  bbb    bbb    2008-9-6B 表数据类似
ID title content date
1  cccc   cccc   2008-9-4
2  ddd    dddd   2008-9-5
3  eee    eee    2008-9-7那么最后select的结果如下
ID Type date
3  1    2008-9-7
2  0    2008-9-6
2  1    2008-9-5
1  0    2008-9-5
1  1    2008-9-4

解决方案 »

  1.   


    if object_id('A')is not null
       drop table A
    if object_id('B')is not null
       drop table B
    go
    create table A
    ( ID int,
      Title varchar(10),
      content varchar(10),
      date datetime
    )
    create table B 
    ( ID int,
      title varchar(10),
      content varchar(10),
      date datetime
    )
    insert into A select 1,'aaa','aaa','2008-9-5'
        union all select 2,'bbb','bbb','2008-9-6'
    insert into B select 1,'cccc','cccc','2008-9-5'
        union all select 2,'ddd','dddd','2008-9-5'
        union all select 3,'eee','eee','2008-9-7'
    select * from (
    select id,type=0 ,date from a
    union all
    select id,type=1,date from b)a
    order by date desc,id desc3 1 2008-09-07 00:00:00.000
    2 0 2008-09-06 00:00:00.000
    2 1 2008-09-05 00:00:00.000
    1 0 2008-09-05 00:00:00.000
    1 1 2008-09-05 00:00:00.000
      

  2.   


    SELECT ID,0 AS TYPE,DATE FROM A
    UNION ALL
    SELECT ID,1 AS TYPE,DATE FROM B
    ORDER BY DATE DESC
      

  3.   

    參考3樓!MySQL應該差別不大的。