日期:2014-05-18  浏览次数:20500 次

sql语句问题一天了!也没有解决!郁闷啊!
表   AA
id     price        
1         50          
2         40          
表BB(注:表AA   id   与   BB   class_id   相关联)
id     class_id    
1             1                
2             2            
3             1              
4             1  
5             1  
我要的结果是:
统计出BB表中   class_id的次数(注:BB表中class_id   与AA表中的ID相关联   )
如:
    id             class_id(统计)
    1                     4
    2                     1
查询   AA   表中   所有记录   按BB表中   class_id(统计)   排序  
select   *   from   AA   order   by   class_id(统计)
我的想法就是这样的!
具体怎么写!还请高手指一下了!!


------解决方案--------------------
select class_id,count(*)
from BB
group by class_id
order by 1
------解决方案--------------------
select id,(select cladd_id,count(*) from bb where bb.cladd_id=aa.id group by claddd_id)as class_id from aa
没测试。楼主可是试试
------解决方案--------------------
select id,t.c from AA left join
(select class_id as id,count(*) as c
from BB
group by class_id
) t
on AA.id=t.id
------解决方案--------------------
这是几个问题?两个?一个?
------解决方案--------------------
你的问题提的不怎么样,哈

答案一
liuyann(liuyann)
select class_id,count(*)
from BB
group by class_id

答案2
select * from AA a
order by (select count(*) from bb b where aa.id = bb.class_id)


------解决方案--------------------
楼主感觉上面的丝路你应该明白了吧,自己试试吧,在家无法帮你测试
------解决方案--------------------

select aa.id ,
count(1) as 'class_id(统计) '
from aa ,
bb
where aa.id = bb.class_id