有两个数据库表
a_users字段包括:id,name....
b_gzjl字段包括:id,p1,p2,p3,p4,p5,p6,nr....
其中b_gzjl中的p1...p6是a_users中的id,现在要统计a_users中的每个id在b_gzjl中出现的次数(所有行和p1...p6列都要统计)

解决方案 »

  1.   

    select count(*),tmp from (
    select id,p1 tmp from b_gzjl
    union
    select id,p2 from b_gzjl
    union
    select id,p3 from b_gzjl
    union
    select id,p4 from b_gzjl
    union
    select id,p5 from b_gzjl
    union
    select id,p6 from b_gzjl
    ) tb group by tb.tmp
      

  2.   

    select count(*),tmp from (
    select id,p1 from b_gzjl
    union
    select id,p2 from b_gzjl
    union
    select id,p3 from b_gzjl
    union
    select id,p4 from b_gzjl
    union
    select id,p5 from b_gzjl
    union
    select id,p6 from b_gzjl
    ) tb group by tb.tmp
      

  3.   

    select 
      a.p1,b.p2,c.p3,d.p4,e.p5,f.p6 
    from 
    (select count(p1) from b_gzjl where p1='條件') a,
    (select count(p2) from b_gzjl where p2='條件') b,
    (select count(p3) from b_gzjl where p3='條件') c,
    (select count(p4) from b_gzjl where p4='條件') d,
    (select count(p5) from b_gzjl where p5='條件') e,
    (select count(p6) from b_gzjl where p6='條件') f,
      

  4.   

    select 
      a.p1,b.p2,c.p3,d.p4,e.p5,f.p6 
    from 
    (select count(p1) as p1 from b_gzjl where p1='條件') a,
    (select count(p2) as p2 from b_gzjl where p2='條件') b,
    (select count(p3) as p3 from b_gzjl where p3='條件') c,
    (select count(p4) as p4 from b_gzjl where p4='條件') d,
    (select count(p5) as p5 from b_gzjl where p5='條件') e,
    (select count(p6) as p6 from b_gzjl where p6='條件') f
      

  5.   

    select a_users.id, count(b_gzjl.id) as times from b_gzjl,a_users where a_users.id=b_gzjl.p1 or a_users.id=b_gzjl.p2 or a_users.id=b_gzjl.p3 or a_users.id=b_gzjl.p4 or a_users.id=b_gzjl.p5 or a_users.id=b_gzjl.p6 group by a_users.id
      

  6.   

    是不是在中文状态下输入呢?SQL SERVER 不直接中文状态下的字符!
    二楼的SQL在我的机子可以运行,而且无错误
      

  7.   

    to:zjjszw(【湘:張家界】穿裤叉的蚊子【抵制日貨】) 
    我知道应该去掉f后的","还是不行,不知道为什么会显示语法错误。
    我是在PB里执行这条sql的,用odbc连接sybase数据库。
    ——————————————————————————
     jmac(jqcca) 的语句是正解
    马上给分!感谢各位!