Home > Database > Mysql Tutorial > 单个select语句实现MySQL查询统计次数_MySQL

单个select语句实现MySQL查询统计次数_MySQL

WBOY
Release: 2016-06-01 13:16:39
Original
1201 people have browsed it

单个select语句实现MySQL查询统计次数

单个select语句实现MySQL查询统计次数的方法用处在哪里呢?用处太多了,比如一个成绩单,你要查询及格得人数与不及格的人数,怎么一次查询出来?
MySQL查询统计次数简单的语句肯定是这样了:


select a.name,count_neg,count_plus from   
(select count(id) as count_plus,name from score2 where score >=60 group by name) a,  
(select count(id) as count_neg,name from score2 where score where a.name=b.name  

即必须至少用2个语句。

今天刚好碰到发现mysql支持if,那就创造性的用if来实现吧:


select name, sum(if(score>=60,1,0)),sum(if(score

单个select语句实现MySQL查询统计次数的方法简单吧。

原理就是大于60,就赋值为1,那么sum就是计数了。

Mysql查询统计函数中的count

今天我遇到一个题目:统计所有女生成绩大于90以上有总数
我刚开始就这样写:$sql = "select 女生成绩 from use where 成绩 > 90“;$result = mysql_query($sql);
$row = mysql_num_rows($result);echo "总数为:$row";
可是100条还行吧,如果是10000条那是不是要很慢啊!!后来一个朋友给我说用count函数,这我才想起来。
把上面的sql语句改为:
$sql = "select count(*),女生成绩 from use group by 女生成绩 having 女生成绩 > 90";
这样查询语句就快多了 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template