统计胜负结果的SQL语句,这里采用子查询来实现。
表结构和数据
date result
2011-02-01 胜
2011-02-01 负
2011-02-01 胜
2011-02-02 胜
2011-02-02 负
采用子查询
select date,(select count(*) from table1 where date = t.date and result = '胜') as '胜'
,(select count(*) from table1 where date = t.date and result = '负') as '负'
from table1 as t group by date;
查询出来的结果:
date 胜 负
2011-02-01 2 1
2011-02-02 1 1
听同学和同事说,这道题有很多公司都作为SQL的笔试题。