读书人

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

发布时间: 2012-04-12 15:46:35 作者: rapoo

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 

读书人网 >SQL Server

热点推荐