有两个表T1,T2
T1有字段age,
T2有字段age我想统计这两个表age字段,归类分组(两个表相同值的归到一组),并统计每一类的行数。

解决方案 »

  1.   

    select age,sum(no) as sno
    from (
    select age,count(1) as no
    from T1
    uinon 
    select age,count(1) as no
    from T2)T
    group by age
      

  2.   


    select sum(id),age
    from (
    select * from T1
    union
    select * from T2) as a
    group by age
      

  3.   

    select 'group1' as groupName,COUNT(*) as groupCount 
    from t1 where exists (select 1 from t2 where t1.age=t2.age)
    union 
    select 'group2',SUM(group2)
    from (
    select COUNT(*) as group2 from t1 where not exists (select 1 from t2 where t1.age=t2.age)
    union select COUNT(*) as group2 from t2 where not exists (select 1 from t1 where t1.age=t2.age)
    )t
      

  4.   


    稍加一下改动select T.age,sum(no) as sno 
    from 
    ( select t2.age,count(t2.age) as no from Table2 t2
    group by t2.age
      union  
      select t3.age,count(t3.age) as no from Table3 t3
    group by t3.age
    )T 
    group by T.age
      

  5.   

    新手 , 目前在自学, 我想知道sum(no)指代什么, 大神勿喷, 往回复。
      

  6.   

    所有no相加啊。楼主先看子查询查出来的结果就知道为什么用sum了。