然后两个表对应的列进行比较取最小值存到表三
关键在这句啊,怎么比较,传说中的ifelse语句,拜托帮写一下,学习学习
连接不是问题,这样吧就当是一个表中两列的数据同行比较如何做来?

解决方案 »

  1.   


    select a.id,timestamp_min=case when a.timestamp>=b.timestamp then b.timestamp else a.timestamp end 
    from table1 a inner join table2 b on a.id=b.id
      

  2.   

    select distinct isnull(table1.id,table2.id),(case when table1.timestamp<table2.timestamp then table1.timestamp else    table2.timestamp end  ) as timestamp
    from table1 full outer join table2 on 
    table1.id=table2.id
      

  3.   

    select id,min(timestamp) as timestamp
    from
    (
    select * from table1
    union all
    select * from table2
    )tt
    group by id
      

  4.   


    select distinct isnull(a.id,b.id) as id ,(case when a.timestamp<b.timestamp then a.timestamp else    b.timestamp end  ) as timestamp_min
    from table1 a full outer join table2 b  on 
    a.id=b.id
      

  5.   

    create table table1 (id int,     timestamp                int)
    insert table1 
    select 1,3           
    union select 2,5
    union select 3,1  
                       create table table2( id  int,   timestamp int)
    insert table2
    select 1, 2 
    union select  2  , 3 
    union select  3  , 4 
    union select  4  , 6 select id=isnull(a.id,b.id) ,timestamp_min=case when a.timestamp>=b.timestamp then b.timestamp else isnull(a.timestamp,b.timestamp) end 
    from table1 a full join table2 b on a.id=b.id---如果要插入到tabel3用这个
    select  id=isnull(a.id,b.id),timestamp_min=case when a.timestamp>=b.timestamp then b.timestamp else isnull(a.timestamp,b.timestamp) end into table3
    from table1 a full join table2 b on a.id=b.idselect * from table3
    ---插入结束drop table table1,table2
      

  6.   

    select id,min(timestamp) as timestamp
    from
    (
    select * from table1
    union all
    select * from table2
    )tt
    group by id这样就可以了吧?不用再连吧?
      

  7.   

    select id = case WHEN a.id is null then b.id else a.id end,
    num = case WHEN a.num < b.num then a.num else b.num end
    from table1 a full join table2 b on a.id =b.id
      

  8.   

    bingo
    leo_lesley(leo)一贴已经搞定,初学sql见笑,下面还有这么多热心的朋友回啊,以后有问题还来这里问啊,哈哈,谢谢大家了