一个sql问题的解决

表内容:   

2005-05-09 胜   

2005-05-09 胜   

2005-05-09 负   

2005-05-09 负   

2005-05-10 胜   

2005-05-10 负   

2005-05-10 负  

输出:

   比赛时间  胜 负   

2005-05-09 2 2   

2005-05-10 1 2  

自己完成建表语句,插入语句

create table bishai(
	id int(11) AUTO_INCREMENT, 
	time varchar(64),
	fengshu int(4),
	primary key(id)
);
insert into bishai(time,fengshu) values('2005-05-09','胜'),('2005-05-09','胜'),('2005-05-09','负'),('2005-05-09','负')
,('2005-05-10','胜'),('2005-05-10','负'),('2005-05-10','bishai负');

 注意这个地方,使用了多个values,使用带有多个VALUES列表的INSERT语句一次插入几行这将比使用一个单行插入语句快几倍。

我的sql如下:

select time as '比赛时间', 
sum(case  when fengshu = '胜' then 1 else 0 end) '胜',
sum(case when fengshu = '负' then 1 else 0 end) '负'
from bishai
group by time
order by time;

猜你喜欢

转载自asialee.iteye.com/blog/1984983