如保统计出一个表中
如 aa表
id user_Id class_Id
1 20 10
2 20 10
3 20 20
4 30 20select count(*) as n where user_id=20
统计出 user_id=20 有多少条记录(但class_id不能有重复的)
统计结果应为 n=2
如 aa表
id user_Id class_Id
1 20 10
2 20 10
3 20 20
4 30 20select count(*) as n where user_id=20
统计出 user_id=20 有多少条记录(但class_id不能有重复的)
统计结果应为 n=2
(
select user_Id,class_Id from tb group by user_Id,class_Id
)a
where user_Id=20
create table tb(id int,user_Id int,class_Id int)
insert tb select 1,20,10
union all select 2,20,10
union all select 3,20,20
union all select 4,30,20select count(user_Id) from
(
select user_Id,class_Id from tb group by user_Id,class_Id
)a
where user_Id=20
-----------
2(所影响的行数为 1 行)
1 20 10
2 20 10
3 20 20
4 30 20select count(*) as n from (
select distinct class_Id,user_id from 表 where user_id=20 ) a
insert tb select 1,20,10
union all select 2,20,10
union all select 3,20,20
union all select 4,30,20select count(*) from (select distinct user_td from tb) w