mysql数据库(三):查询的其他用法

一. 查询—IN的用法

语法:select ... from 表名 where 字段 a in (值b, 值c, 值d...)

等价于 select ... from 表名 where 字段a=值b or 字段a=值c or 字段a=值d;

例如,查询学生表里学生编号为1401001或者1401002或者1401003的学生信息

select * from student where id=1401001 or id=1401002 or id=1401003;

select * from student where id in(1401001, 1401002, 1401003);

二. 查询—模糊查询(LIKE)

语法:select <字段1, 字段2, ...> from <表名> where <字段名> like '%值a%';

示例:

查询学生表中姓名中有"小"的学生信息

分析问题:

  • 涉及表:学生表

  • 要显示的字段:学生表中所有字段

  • 条件:学生姓名中有"小"字

  • 编写如下sql:

    select * from student where name like '%小%';

注意:

%在不同位置所代表的意义  %值  值%  %值%

%A:以任意字符开头,以A结尾的字符串

A%:以A开头,以任意字符结尾的字符串

%A%:包含了A的任意字符串

练习:

1. 查询科目表中,科目名称有"语"的科目信息

三. 查询—统计(COUNT(*))

关键词:count(*)

语法:select count(*) from <表名> where 条件表达式;

1. 统计一个表里总共有多少条记录

举例:查询学生表总共有多少学生

select count(*) from student;

2. 统计满足某一条件的记录数

举例:查询学生表的女生数量

select count(*) from student where sex='女';

3. 统计高一年级下共有多少学生

select count(*) from grade t1, class t2, student t3 where t3.class_id = t2.id and t2.grade_id=t1.id and t1.id=(select id from grade where name='高一年级');

select count(*) from student where class_id in (select t1.id from class t1, grade t2 where t1.grade_id=t2.id and t2.name='高一年级');

select count(*) from grade t1, class t2, student t3 where t3.class.class_id=t2.id and t2.grade_id=t1.id and t1.name='高一年级';

四. 查询—分组(GROUP BY)

根据一个或多个列对结果集进行分组

语法:select 字段1, 字段2, 统计函数xx() from <表名> group by 字段1, 字段2

select

示例:请按性别分组,统计学生表里男生和女生各有多少人

分析:

涉及表:student

查询字段:sex, count(*)

selct sex, count(*) from student group by sex;

五. MySQL—BETWEEN的用法

找出score表成绩在80和90之间的学生(包含边界值80和90)

select * from score where score between 80 and 90;

六. MySQL分页

语法:limit m,n;

m指的索引值是从m开始,n表示每页要取多少条

假如每页取十条展示,则第一页为:limit 0,10表示取索引从0开始取10条记录,第二页为:limit 10,10 表示取索引从10开始取10条记录,第三页为:limit 20,10 表示索引从20开始取10条记录

1) 请用sql查询出student表的前10条记录

2) 请用sql查出student表的第10到15条记录

七. 常见MySQL函数

举例mysql中常用的sql函数

数值相关函数

求字段A的最小值:min(字段A)

求字段A的最大值:max(字段A)

求字段A的平均值:avg(字段A)

求字段A的和:sum(字段A)

日期函数

获取系统当前日期时间:sysdate()

获取系统当前日期:curdate()

获取系统当前时间:curtime()

获取date是一个月的第几天:dayofmonth(date)

获取当前月的最后一天:last_day(date)

为日期增加一个时间间隔:DATE_ADD(date, INTERVAL expr unit),例如在当前日期上加一天:select DATE_ADD(CURDATE(), INTERVAL 1 day);

获取当前月第一天:select date_add(curdate(), interval -day(curdate())+1 day);

字符串函数:

字符串拼接函数:concat(字段A, 字段B), SUBSTR(字段A, 截取开始的位置,截取字符个数), length(字段A)

八. 课后作业

1. 查询出"高一年级"下面的所有班级里面的男学生信息;

select t3.* from grade t1,class t2,student t3 where t1.id=t2.grade_id and t2.id=t3.class_id and t1.name='高一年级' and t3.sex='';

2. 查询成绩小于等于90分的学生姓名、性别、科目名称、分数(涉及表student、course、score)

select t1.name,t1.sex,t2.name,t3.score from student t1,course t2,score t3 where t1.id=t3.student_id and t2.id=t3.course_id and t3.score between 0 and 90;

3. 查询高二年级下所有数学成绩小于90分的同学的学号和姓名以及分数

九. 常见面试题

1. 图书(图书号,图书名,作者编号,出版社,出版日期) 作者(作者姓名,作者编号,年龄,性别) 用SQL语句查询年龄小于平均年龄的作者姓名、图书名、出版社

2. 作者表:authors

作者编号:authorId  int(11)

作者姓名:authorName  varchar(50)

性别:sex  varchar(2)

年龄:age  int

居住城市:city  varchar(50)

联系电话:telephone  varchar(11)

销量:sales  int(11)

最新出版日期:jsbn  datetime

1) 查询姓张的作者信息

2) 查询联系电话第三位为8,9并以888结尾的作者信息

3) 查询年龄在20-50之间的男性作者信息

4) 查询显示作者姓名的第二个字符

5) 查询显示作者姓名的长度

6) 查询显示最年轻的5位作者的平均销量

7) 查询显示作者的姓名,出生年份,销量,并按销量降序排列

8) 查询显示最新出版日期在今年前半年的作者信息

猜你喜欢

转载自www.cnblogs.com/cnhkzyy/p/9463075.html