有2张表
TB_FACTURE表
      USER_ID列
TB_MUSIC表
      USER_ID列
查询出2张表 USER_ID列中 同一个数据出现了多少遍,主要为了统计 比如:
TB_FACTURE表                               TB_MUSIC表
         USER_ID列                                     USER_ID列
数据为:    1                                              1
            2                                              4
            3                                              5
            1                                              2
            2                                              3
            3                                              4
            1                                              2
            2                                              2
            3                                              4
            1                                              34
            2                                              10
            3                                              23
            1

解决方案 »

  1.   


    select USER_ID,count(*)
    from tb
    group by USER_ID
      

  2.   

    select a.count(1),b.count(1) from TB_FACTURE a,TB_MUSIC b where a.USER_ID =b.USER_ID
      

  3.   

    比如:
    TB_FACTURE表 TB_MUSIC表
      USER_ID列 USER_ID列
    数据为: 1 1
      2 4
      3 5
      1 2
      2 3
      3 4
      1 2
      2 2
      3 4
      1 34
      2 10
      3 23
    你要的结果是什么?
      

  4.   

    执行为什么报错呢??消息 4121,级别 16,状态 1,第 1 行
    找不到列 "a" 或用户定义的函数或聚合 "a.count",或者名称不明确
      

  5.   

    select *
    (select count(1) as tb1count from tb1 group by USER_ID) a
    full join 
    (select count(1) as tb2count from tb2 group by USER_ID) b
    on a.USER_ID=b.USER_ID
      

  6.   

    select a.USER_ID,a.cou1,b.cou2 from 
    (select USER_ID,count(*) cou1
    from TB_FACTURE表
    group by USER_ID) a,
    (select USER_ID,count(*) cou2
    from TB_MUSIC表
    group by USER_ID) b
    where a.USER_ID=b.USER_ID
      

  7.   


    比如:(一条语句)
    userID    showCount
    1           13
    2            6
    4            3
    10           1
      

  8.   


    select user_id,sum(cn)
    from(select uesr_id,count(*) as rn from tb1 group by user_id
        union all
         select uesr_id,count(*) as rn from tb2 group by user_id)t
    group by user_id
      

  9.   

    select user_id,sum(rn)
    from(select uesr_id,count(*) as rn from tb1 group by user_id
        union all
         select uesr_id,count(*) as rn from tb2 group by user_id)t
    group by user_id
      

  10.   

    谢谢你! 成功了!!  这是什么查询方法? union all  师傅,指教下啊。
      

  11.   


    你写的 跟 教我对的那个 人 就有一个地方不一样
    你用的full join
    他用的union all这个是什么意思?
      

  12.   

    Create table #tt(userid int) 
        Create table #t(userid int) 
      insert into #tt values(3)
        insert into #tt values(3)
          insert into #tt values(5)
          
            insert into #t values(3)
        insert into #t values(3)
          insert into #t values(5)
      
      
      
        select * from #tt
        select * from #t
        select userid,SUM(userid) from (
       select * from #tt
       union all
        select * from #t
    ) A group by userid
        
      
      

  13.   

    union all  是记录的合并,将几个查询记录并到一起,必须是字段的数据类型一致,字段个数一致才能用union
      

  14.   

    将记录合并到一起? int类型的 直接相加? varchar 就不变了?
    还有
    select USER_ID,sum(rn)
    from(select USER_ID,count(*) as rn from TB_FACTURE group by USER_ID
        union all
         select USER_ID,count(*) as rn from TB_MUSIC group by USER_ID)t
    group by USER_ID这个语句最后 group by 上面还有一个 小t 这个是干吗的?