1. 程式人生 > >sql語句裡的if用法例項

sql語句裡的if用法例項

<strong><span style="font-size:18px;">//資料庫test中的表student。欄位name,class,score,分別代表姓名,班級,分數。
//1.算出每個班級中的學生,按照成績降序排序
/*
select name,classa,score fron student order by classa,score desc;

create table student(
id int(10) not null auto_increment,
name varchar(30) not null,
classa varchar(30) not null,
score int(10) not null, 
primary key(id)
)charset=utf8;
INSERT INTO `student`(`id`, `name`, `classa`, `score`) VALUES ('','小高',1,30);
INSERT INTO `student`(`id`, `name`, `classa`, `score`) VALUES ('','小裡',2,40);
INSERT INTO `student`(`id`, `name`, `classa`, `score`) VALUES ('','小是',3,50);
INSERT INTO `student`(`id`, `name`, `classa`, `score`) VALUES ('','小上',1,60);
INSERT INTO `student`(`id`, `name`, `classa`, `score`) VALUES ('','小的',2,70);
INSERT INTO `student`(`id`, `name`, `classa`, `score`) VALUES ('','小他',3,80);
INSERT INTO `student`(`id`, `name`, `classa`, `score`) VALUES ('','小的',1,90);
INSERT INTO `student`(`id`, `name`, `classa`, `score`) VALUES ('','小高',2,50);
INSERT INTO `student`(`id`, `name`, `classa`, `score`) VALUES ('','小高',3,40);
//2.輸出每個班和班對應及格人數和不及格人數
select classa,sum(if(score>=60,1,0)) jige,sum(if(score<60,1,0)) bujige from student 
group by classa;
*/</span></strong>