zoukankan      html  css  js  c++  java
  • 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分的同学的学号和姓名以及分数(涉及到五个表:grade,class,student,course,score)

    select t3.id,t3.name,t5.score from grade t1,class t2,student t3,course t4,score t5 where t1.id=t2.grade_id and t2.id=t3.class_id and t3.id=t5.student_id and t4.id=t5.course_id and t1.name='高二年级' and t4.name='数学' and t5.score<90;

    九. 常见面试题

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

    select 作者.作者姓名,图书.图书名,图书.出版社 from 图书,作者 where 图书.作者编号=作者.作者编号 and 作者.年龄<(select avg(年龄) from 作者);

    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) 查询姓张的作者信息

    select * from authors where authorName like '张%';

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

    select * from authors where substr(telephone,3,1) in ('8','9') and telephone like '%888';

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

    select * from authors where age between 20 and 50 and sex='男性';

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

    select substr(authorName,2,1) from authors;

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

    select len(authorName) from authors;

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

    select avg(t.sales) from (select * from authors order by age asc limit 0,5)) t;

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

    select authorName, year(curdate())-age, sales from authors order by sales desc;

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

    select * from authors where month(isbn) <7;
  • 相关阅读:
    横冲直撞vue(第六篇):vue之过滤器、es6中填充字符串、es6新增的padStart()方法和padEnd()方法、vue自定义键盘修饰符、vue自定义全局指令
    leetcode的奇妙冒险(python3)系列:leetcode 283. Move Zeroes
    横冲直撞vue(第五篇):事件修饰符、指令系统综合案例
    横冲直撞vue(第四篇):v-model、指令系统总结、指令系统示例轮播图实现、指令系统示例跑马灯效果实现、在vue中使用样式的方式
    横冲直撞vue(第三篇):vue中template的三种写法、v-bind、v-on、更新元素的指令v-text与v-html、条件渲染指令v-if 与v-show、v-for
    横冲直撞vue(第二篇):什么是vue?框架和库的区别、vue的优点、vue的使用、使用vue实例化对象
    横冲直撞vue(第一篇):常用的ES6语法
    nodejs(第五篇):npm常用命令、包说明文件package.json、packjson-lock.json文件、使用nodemon插件、nrm的安装与使用
    最详细的个人博客教程搭建教程,最快5分钟快速搭建简约风格博客
    面试问了解Linux内存管理吗?10张图给你安排的明明白白!
  • 原文地址:https://www.cnblogs.com/my_captain/p/9463075.html
Copyright © 2011-2022 走看看