關(guān)于group by 的應(yīng)用問題
數(shù)據(jù)庫內(nèi)容為下面

寫一SQL得出下面內(nèi)容:

貼出SQL結(jié)果如下:(MySQL版本)
create table gosin_temp(rq varchar(10),shengfu nchar(1));
insert into gosin_temp values('2009-05-09','勝');
insert into gosin_temp values('2009-05-09','勝');
insert into gosin_temp values('2009-05-09','負');
insert into gosin_temp values('2009-05-09','負');
insert into gosin_temp values('2009-05-10','勝');
insert into gosin_temp values('2009-05-10','負');
insert into gosin_temp values('2009-05-10','負');
select * from gosin_temp;
得到結(jié)果的SQL:
select a1.rq,a1.勝,b1.負 from
(select a.rq, count(a.shengfu) 勝 from gosin_temp a where a.shengfu='勝' group by a.rq) a1,
(select b.rq, count(b.shengfu) 負 from gosin_temp b where b.shengfu='負' group by b.rq) b1
where a1.rq = b1.rq
類似的題目還有很多,如:
勝 負
1 a b
2 b a
3 b a
要求寫一SQL語句,輸出如下結(jié)果:
勝 負
a 1 2
b 2 1
其實都一樣 只要熟悉使用group by 就不覺得難了。