首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL查询语句 去重复解决方案

2012-04-12 
SQL查询语句 去重复a表中各个科目的数据都有n条现在要统计表中有哪些科目及各科的条数我是这样写的select

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
。。。。。。。

重复了


[解决办法]

SQL code
select subject=t2.code_name,num=COUNT(*)from t_tutor_phone_log t1      inner JOIN t_code AS t2 ON t1.SUBJECT=t2.code_idWHERE [USER_ID]=133 GROUP BY t2.code_name 

热点排行
Bad Request.