请高手指点一个sql语句的写法!
表1 AA
id class_id user_id addtime
1 20 100 2007-1-1
2 20 50 2007-1-2
3 20 50 2007-1-3
4 30 100 2007-1-4
表2 BB
user_id name
50 aa
100 bb
结果
class_id user_Id addtime cont(统计) username
20 100 2007-1-1 1次 bb
20 50 2007-1-3 2次 aa
------解决方案--------------------select
a.*,b.name as username
from
(select
class_id,
user_Id,
count(*) as cont,
max(addtime) as addtime
from
AA
group by
class_id,user_Id) a,
BB b
where
a.user_id=b.user_id
and
a.class_id=20
------解决方案----------------------try
select class_id,user_Id,addtime,username,cont(user_id)
from aa a left
join bb b on a.user_id=b.user_id
group by class_id,user_Id,addtime,username
------解决方案--------------------select
a.class_id,
a.user_Id,
count(a.user_Id) as cont,
max(a.addtime) as addtime,
b.name as username
from
AA a,BB b
where
a.user_id=b.user_id
and
a.class_id=20
group by
a.class_id,a.user_id,b.name
------解决方案--------------------select a.*,t2.name from (select class_id,user_id,max(addtime)addtime,count(*)as [count(统计)] from t1
where class_id=20
group by class_id,user_id) as a ,t2 where a.user_id = t2.user_id
------解决方案---------------------- 表1 AA
-- id class_id user_id addtime
-- 1 20 100 2007-1-1
-- 2 20 50 2007-1-2
-- 3 20 50 2007-1-3