1,
select * from score where score>95; //显示所有分数在95分以上的信息
select last_name,first_name from president where last_name=’Tom’; //找出所有姓tom的总统
select last_name,first_name,birth,state from president
where birth<’1950-1-1’ and (state=’VA’ or state=’BA’);
2,
select last_name,first_name,birth,state from president
where death is null; //找出所有没死的总统
3,
select last_name,first_name,birth,state from president
order by last_name; //让总统们的名字按字母顺序排列
select last_name,first_name from president
order by state DESC,last_name ASC;
4,
select last_name,first_name,birth,state from president
order by birth limit 5;
order by birth limit 10,5; //返回从第11个记录开始的5个记录(跳过了10个)
select last_name,first_name,birth,state from president
order by rand() limit 1; //这是用了表达式求值的方法,在哪里都管用
5,
Select 17,format(sqrt(3*3+4*4),0))
select concat(first_namem,’ ‘,last_name),concat(city,’,’,state) from president;
如果合并之后输出列的标题过长,则可以给其一个别名,如:
select concat(first_namem,’ ‘,last_name) as name,
concat(city,’,’,state) as birth place
from president;这样就比较美观了。
6,
select * from event where date=’2002-10-01’ //看看这天有何考试信息?
select last_name,first_name,birth,state from president
where death>’1900-01-01’ and death<’2000-01-01’; //看看上个世纪死了几个?
select last_name,first_name,birth from president
where month(birth)=3; //谁生在3月 ?
where month(birth)=7 and dayofmonth(birth) =6; //谁生在7月6日?(汤姆克鲁斯?)
select last_name,first_name,birth to_days(death)-to_days(birth) as age from president
select last_name,first_name,expiration from member
where (to_days(expiration)-to_days(curdate())<60; //有些人60天内需要花钱了!
7,
select concat(first_namem,’ ‘,last_name) as name,
where last_name like ‘W%’; //找到以W或w开头的人
where last_name like ‘%W%’; //找到名字里面W或w开头的人
8,
select @birth:=birth from president
where last_name =’adsltiger’;
select concat(first_namem,’ ‘,last_name) as name from president
where birth<@birth order by birth; //看看那些人比我大!
*9,
9.1 找出一组数据中到底有多少种不同的值是一项比较常见的统计工作,而关键字distinct就可以把查询结果中的重复数据清除掉。如
9.2用count()函数来统计相关记录的个数,注意其使用方法:count(*)计算所有的,NULL也要;count(数据列名称) NULL值不计算在内。
select count(*) from president;
9.3如果我们想知道班级内的男女生数目?该如何查询呢?最简单的方法是
select count(*) from student where sex=’f’;
select count(*) from student where sex=’m
select sex,count(*) f rom student group by sex;
select state,count(*) as count from president
group by state order by count desc limt4; //看看出生总统最多的前四个州是哪几个?
9.4除了count(),我们还用其他一些统计函数,如求出最小值的min(),求最大值的max(),求和的sum(),求平均值的avg(),在实际工作中,这些函数时经常用到的!
*10,
例子1:查询某一天内的学生们的考试成绩,用学号列出。
select scroe.student_id,event_date,score.score.event.type
from event,score
where event.date=’2003-09-12’
and event.event_id=score.event_id
例子2:查询某一天内的学生们的考试成绩,用姓名列出。
select student.name event.name,score.score,event.type
form event,score,student
where event.date=’2003-09-12’
and event.event_id= score.event_id
and scroe.student_id=student.student_id;
例子3:查询一下缺席学生的名字,学号,缺席次数
select student.student_id,student_name
count(absence.date) as absences
from student,absence
where student.student_id=absence.student_id
group by student.student_id;