如A表中有
id class
1  工业
2  商业
3  农业B表有
id A_id city
1  1    沈阳
2  1    西安
3  2    广州
4  1    长春
5  3    安山
6  3    绵阳
7  2    深圳
8  1    东莞然后查询出来是
id class count_city
1  工业  4
2  商业  2
3  农业  2谢谢

解决方案 »

  1.   

    select a.*,cont(b.a_id) as cont_city from a,b
    where a.id=b.a_id
      

  2.   

    select a.*,isnull(cnt,0) count_city
    from ta a
    left join
    (select a_id,count(*) cnt from tb group by a_id) b
    on id=a_id
      

  3.   

    select a.*, count_city from a join (select aid, count(*) count_city from b group by aid) b on id=aid
      

  4.   

    from a,b where ..
    from a join ..
    都是inner join的写法.(join 默认操作为inner join. from a,b where 会转换为inner join来操作)
    由于b中可能不出现对应于a中某条记录的数据, 所以inner join可能会导至减少行.