日期:2014-05-17  浏览次数:20345 次

SQL查询语句 去重复
本帖最后由 yylyhl 于 2012-03-28 10:02:52 编辑
a表中各个科目的数据都有n条  现在要统计表中有哪些科目及各科的条数 
  我是这样写的
select subject=(select code_name from t_code where code_id=t1.subject),
num=(select count(*) from t_tutor_phone_log where user_id=133 and subject=t1.subject)
from t_log t1 
但是这样查到的结果是:
   subject num
1   历史    2
2   英语    1
3   历史    2
4   地理    2
。。。。。。。

重复了

------解决方案--------------------
select 
subject=t2.code_name,
num=COUNT(*)
from t_tutor_phone_log t1  
inner JOIN t_code AS t2 ON t1.SUBJECT=t2.code_id
WHERE [USER_ID]=133 
GROUP BY t2.code_name