2011-05-30 36 views
1

的两列获得最大的我有两个表从两个表

T1

id  name 
1  saanu 
3  abc 

delT1

id name 
2  val2 
4  val4 

我必须从两个找出ID(INT)的最大值表。

+0

什么格式???? – sanu 2011-05-30 17:40:22

回答

8
select max(maxId) from 
(
    select max(id) as maxId from delT1 
    union all 
    select max(id) as maxId from t1 
) maxes; 
+0

它没有工作它给出“不正确的语法附近')'”error.by我需要的方式MS SQL服务器2005 – sanu 2011-05-30 18:00:12

+0

@mu太短:10秒是您正确张贴派生表上的别名的地方.. – gbn 2011-05-30 18:19:00

+0

@gbn:好吧,我的SQL并不是无效的,因为op没有指定他正在使用的DBMS ... – 2011-05-30 20:05:18

0
select mymax = 
    case 
     when MAX(id1) > MAX(id2) then MAX(id1) 
     when MAX(id2) > MAX(id1) then MAX(id2) 
     end 
     from table1,table2