赞
踩
select 项的原名 as 别名
例如:
select studentno as '学号',phone as '手机号' from student
如果希望一个列表没有重复值,可以利用distinct从结果集中除去重复行。
例如:在score表中查询期末成绩有高于80分的学生的学号,不管有几门课的成绩高于95,只要有一门就可以显示,利用distinct可将重复行除去。
select distinct studentno from score where final>80;
select studentno,sname,phone from student where entrance>800;
in关键字可以判断某个字段的值是否在指定的集合中,也可以使用not in关键字查询不在某取值范围内的记录行数据。
例如:查询学号分别为18122221324,18122210009的成绩信息。
select * from score where studentno in('18122221324','18122210009');
使用between 搜索条件相当于使用and连接两个比较条件;
检索条件指定排除某个范围的值,一般可以利用not between关键字来实现。
例如:查询选修课程号为c05109的学生学号和期末成绩,并且要求平时成绩在80-95分。
select studentno,final from score where courseno='c05109' and daily between 80 and 95;
like可以实现模糊查询,有两种通配符:‘%’、‘_’
'%'可以匹配一个或多个字符,可以代表任意长度的字符串,长度可以为0;
'-'只匹配一个字符。
例如:在student表查询所有姓何的学生的姓名和Email。
select sname,Email from student where sname like '何%';
一个字段是空值或者不是空值,要表示为is null或is not null
例如:查询成绩表期末成绩为空的学生信息
select * from score where final is null;
and 需要满足所有的条件;
or 只要符合多条件中的一个,记录就会被搜索出来。
例如:查询计算机学院的具有高级职称教师的教师号、姓名和从事的专业。
select teacherno,tname,major from teacher where department='计算机学院' and (prof='教授' or prof='副教授');
(1)默认情况下,结果集按照升序排序,也可以在输出项的 后面加上关键字desc来实现降序输出;
(2)对含有null值的列进行排序时,如果是按升序排列,null值将出现在最前面,如果是按降序排列,null值将出现在最后;
(3)order by可以同时指定多个排序项;
(4)order by不可以使用text、blob、longtext和mediumblob等类型的列。
例如:在score查询期末成绩大于60分的学生信息,并先按照课程号升序,再按照期末成绩降序排序。
select * from score where final>60 order by courseno,final desc;
select courseno,group_concat(studentno) from score group by courseno;
-> select studentno as '学号', sum(daily+final) as '总分'
-> from score
-> where final>75
-> group by studentno
-> having count(*)>=3
-> order by sum(daily+final) desc;
select * from score where final>60 limit 2,5;
-> select student.studentno as '学号',student.sname as '姓名',score.final as '期末成绩'
-> from student
-> inner join score
-> on student.studentno=score.studentno
-> where score.courseno='c05109';
mysql> select student.studentno as '学号',sname as '姓名',daily as'平时成绩',final as '期末成绩'
-> from student
-> left join score
-> on student.studentno=score.studentno
-> ;
mysql> select teacher.teacherno,tname,major,courseno
-> from teacher right join teach_course
-> on teacher.teacherno = teach_course.teacherno;
mysql> select student.studentno,sname,cname,final,period/16
-> from score join student on student.studentno=score.studentno
-> join course on score.courseno=course.courseno
-> where substring(student.studentno,1,2)='18';
mysql> create table student01 as
-> select studentno,sname,phone from student;
mysql> select studentno,sname,phone from student01
-> where phone like '%131%'
-> union
-> select studentno,sname,phone from student
-> where phone like '%136%';
子查询可以报错in、not in、any、all、exists、not exists等逻辑运算符,也可以包含比较运算符,如 = 、!= 、>、<等。
根据子查询的结果又可以将mysql子查询分为4种类型。
(1)返回一个表的子查询时表子查询。
(2)返回带有一个或多个值的一行的子查询时行子查询。
(3)返回一行或多行,但每行只有一个值的时列子查询。
(4)只返回一个值的是标量子查询。从定义上讲,每个标量子查询都是一个列子查询和行子查询。
例如:查询期末成绩比选修该课程平均期末成绩低的学生的学号,课程号和期末成绩。
mysql> select studentno,courseno,final
-> from score as a
-> where final <(select avg(final)
-> from score as b
-> where a.courseno=b.courseno
-> group by courseno);
例如:获取期末成绩中含有高于93分的学生的学号、姓名、电话、和Email。
mysql> select studentno,sname,phone,Email
-> from student
-> where studentno in (select studentno
-> from score
-> where final>93);
例如:查询student表中是否存在2001年12月12日以后出生的学生,如果存在,输出学生的学号、姓名、生日和电话。
mysql> select studentno,sname,birthdate,phone
-> from student where exists(
-> select * from student
-> where birthdate > '2001-12-12');
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。