当前位置: 代码迷 >> Sql Server >> SQL查询语句 去重复,该怎么处理
  详细解决方案

SQL查询语句 去重复,该怎么处理

热度:45   发布时间:2016-04-24 18:41:59.0
SQL查询语句 去重复
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
  相关解决方案