首页 > 数据库 >MySQL_2

MySQL_2

时间:2022-09-03 16:33:28浏览次数:47  
标签:students age -- SELECT MySQL where select

1. 字段的别名

  • 通过字段名as 别名的语法,可以给字段起一个别名,别名可以是中文,as可以省略
  • 字段名as别名和字段名别名结果是一样的
-- 通过as给字段起一个别名
select card as身份证,name as姓名,sex as 性别 from students;
-- 别名的as可以省略
select card身份证,name姓名,sex 性别 from students;

2. 表的别名

  • 通过表名as别名给表起一个别名
  • as可以省略
-- 通过as给表students起一个别名
select  from students as stu;
-- 可以省略as
select  from students stu;

3. distinct过滤重复记录

  • 通过select distinct字段名,字段名from表名,来过滤select查询结果中的重复记录
SELECT DISTINCT sex,class fr om students;

4.where子句

  • where后面跟一个条件,实现有选择的查询
  • select * from表名where条件
-- 例1:查询students表中学号studentNo 等于"001’的记录
select * from students where studentNo = '001";
-- 例2:查询students表中年龄age等于30的姓名name,班级class
select name,class from students where age = 30;

5.select查询的基本规律

  • select * 或者select 字段名 控制了查询返回什么样的字段(列)
  • where条件控制了查询返回什么样的记录(行)

6.比较运算符

  • <:小于

  • <=:小于等于

  • >:大于

  • >=:大于等于

  • !<>不等于

-- 例1:查询students 表中name(姓名〉等于'小乔"学生的age(年龄)
select age from students where name = '小乔';
-- 例2:查询students表中30岁以下的学生记录
select from students where age < 30;
-- 例2:查询students表中30岁和30岁以下的学生记录
SELECT * from students where age <= 30;
--查询家乡不在'北京'的学生记录
select * from students where hometown !='北京';
select * from students where hometown <> '北京";

7.逻辑运算符

  • and与

    • 条件1 and条件2。
    • 两个条件必须都满足
  • or或

    • 条件1 or条件2

    • 两个条件只要有一个满足即可

  • not非

    • not条件
    • 条件成立,not以后就不成立,条件不成立,not以后就成立。

8.模糊查询

  • like实现模糊查询
  • %代表任意多个字符
  • _代表任意一个字符
  • 字段名 like '字符%'
    • 指定字符开始,后面任意多个字符
-- 例1:查询name姓名中以"孙"开头的学生记录
SELECT from students where name like '孙%';
-- 例2:查询name 姓名以'孙"开头,且名只有一个字的学生记录
SELECT * from students where name like '孙';
-- 例3:查询name为任意姓,名叫'乔”的学生记录
SELECT * from students where name like '%养乔';
-- 查询name姓名有"白’子的学生记录
SELECT 全 from students where name like '%白%';

9.范围查找

  • in(值,值,值)

    • 非连续范围查找
    SELECT * from students where hometown in ('北京','上海','广东');
    
  • between 开始值 and结束值

    • 连续范围查找包含开始值包含结束值
    SELECT * from students where age >= 25 and age <= 30;
    

10.空判断

  • null不是0,也不是" ,null在SQL里面代表空,什么也没有
  • null不能用比较运算符的判断
  • is null ---是否为null
  • is not null ---是否不为null
    • 不能用字段名 = null ; 字段名 != null ; 这些都是错误的
-- 例1:查询card身份证为nu1l的学生记录
SELECT R from students where card is nu11 ;
-- 例2:查询card身份证非nu11的学生记录
SELECT from students where card is not nu11;

11.where子句可以用到update和delete语句后面

-- 例1:修改age 为25,并且 name为'孙尚香"的学生class为”2班”
update students set class = '2班' where age = 25 and name = '孙尚香';

-- 例2:删除class为”1班”,并且age 大于30的学生记录
DELETE from students where class = '1班' and age > 30;

12.order by排序

  • order by 字段名[asc/desc]
    • asc代表从小到大升序,asc可以省略
    • desc代表从大到小降序,不可以省略
-- 例1:查询所有学生记录,按age年龄从小到大排序
select * from students order by age asc;
select from students order by age;
-- 例2:查询所有学生记录,按age年龄从大到小排序
select from students order by age desc;
  • 排序的另一个例子:(一定是先排第一个,按顺序依次排序)
-- 例2:查询所有学生记录,按age年龄从大到小排序,
-- 年龄相同时,再按studentNo学号从小到大排序
SELECT from students ORDER BY age desc, studentNo;
  • 当一条select语句出现了where和order by
    • select * from 表名 where 条件 order by 字段1,字段2;
    • 一定要把where写在order by前面
-- 练习:查询所有男学生记录,按class班级从小到大排序,班级相同时,
-- 再按studentNo学号再按学号从大到小排序
SELECT * from students where sex = '男' order by class,studentNo desc;

13.聚合函数

  • 为了快速得到统计数据,经常会用到如下5个聚合函数
  • 注意聚合函数不能在where后面的条件中使用

where age = max(age) ----> 错误!!!

count求select返回的记录总数

  • count(字段名)
-- 查询学生总数(查询stuents表有多少记录)
select count(*) from student s;
select count(name) from students;
select count (DISTINCT class) from students;
select count (DISTINCT sex) firom students;
-- 查询女同学数量
SELECT count(name) from students where sex = '女';
SELECT count(*) from students where sex ='女';
SELECT count(sex) from students where sex ='女';

max查询最大值

  • max(字段名)
    • 查询指定字段里的最大值
-- 查询students中的最大年龄
SELECT max(age) from students ;
-- 查询students中的女生最大年龄
SELECT max(age) from students where sex = '女';
-- 查询students中的'1班'最大年龄
SELECT max(age)from students where class = '1班';

min查询最小值

  • min(字段名)
    • 查询指定字段的最小值
-- 查询students中的最小年龄
SELECT min(age) from students ;
-- 查询students中的女生最小年龄
SELECT min(age) from students where sex ='女';
-- 查询students中的1班最小年龄
SELECT min(age) from students where class = '1班';

sum求和

  • sum(字段名)
    • 指定字段的值求和
-- 查询students中的年龄总和
SELECT sus(age from students ;
-- 查询students中的女生年龄总和
SELECT sum(age) from students where sex ='女';
-- 查询students中的'1班'年龄总和
SELECT sum(age) from students where class = ‘1班';

avg求平均数

  • avg(字段名)
    • 指定字段的平均值
-- 查询students中的年龄总和
SELECT sus(age) from students ;
-- 查询students中的女生年龄总和
SELECT sum(age) from students where sex ='女';
-- 查询students中的'1班'年龄总和
SELECT sum(age)from students where class = '1班';
  • avg的字段中如果有null,null不做为分母计算平均

14.数据分组

  • group by字段名
  • select 聚合函数 from 表名 where 条件 group by 字段.
  • select 聚合函数 from 表名 group by 字段
  • group by就是配合聚合函数使用的,不用聚合函数的group by没有任何意义
-- 分别查询男女同学的数量
SELECT count(*)from students where sex ='男';
SELECT count(*)from students where sex='女';
select sex,count(i) from students group by sex;
  • group by的例子
-- 分别查询各个年龄段的同学数量
select age,count(*) from students group by age;
  • where与group by
-- 分别查询'1班'不同性别学生数量
select sex,count(*) from students where class = '1班' group by sex;
  • 练习
-- 练习:统计各个班级学生总数、平均年龄、最大年龄、最小年龄。
-- 但不统计'3班',统计结果按斑级名称从大到小排序
SELECT class,count(*),avg(age),max(age), min(age) from studentswhere class '3班' GROUP BY class ORDER BY class desc;

order by是对整个结果进行排序,所以要放在最后

  • where 和 group by 和 order by 的顺序
    • select * from 表名 where 条件 group by 字段 order by字段

15.分组聚合之后的数据筛选

  • having子句
  • 总是出现在group by之后
  • select * from 表名 group by 字段 having 条件
-- 用where查询男生总数
-- where先锦选复合条件的记录,然后在聚合统计
SELECT count(*) from students where sex ='男';
-- 用having查询男生总数
-- having先分组聚合统计,在统计的结果中饰选
SELECT count(*) from students GROUP BY sex HAVING sex = '男';

having配合聚合函数的使用

  • where后面条件不能使用聚合函数, having可以使用聚合函数
-- 求班级人数大于3人的班级名字
select class from students GROUP BY class HAVING count(*) >3;

16.having与where筛选的区别

  • where是对标的原始数据进行筛选
  • having是对group by之后已经分过组的数据进行筛选having
  • 可以使用聚合函数, where不能用聚合函数

17.limit显示指定的记录数

  • select * from 表名 where 条件 group by 字段 order by 字段 limit start, count
  • limit总是出现在select语句的最后,
  • start代表开始行号,行号从0开始编号
  • count代表要显示多少行
  • 省略start,默认从0开始,从第一行开始
-- 查询前三行记录
SELECT  from students limit 0,3;
SELECT  from students limit 3;
-- 查询从第4条记录开始的三条记录
SELECT from students 1imit 3,3;
  • 当有where或者group by或者order by, limit总是出现在最后
-- 查询年龄最大同学的name
select name from students 0RDER BY age desc limit 1;
-- 查询年龄最小的女同学信息
SELECT * from students where sex ='女' ORDER BY age LIMIT 1;

18.数据分页显示

  • m每页显示多少条记录

  • n,第n页

  • (n-1)* m, m

  • 把计算结果写到limit后面

-- 每页显示4条记录,第3页的结果
select from students limit 8,4;
-- 每页显示4条记录,第2页的结果
select  from students limit 4,4;
  • 已知每页记录数求一张表需要几页显示完
    • 求总页数
    • 总页数/每页的记录数
    • 如果结果是整效,那么就是总页数,如果结果有小数,那么就在结果的整数上+1

标签:students,age,--,SELECT,MySQL,where,select
From: https://www.cnblogs.com/lzy5967/p/16652920.html

相关文章

  • 4.2 安装与配置mysql模块
    在使用mysql模块操作mysql数据库之前,必须先对mysql模块进行必要的配置步骤如下:constmysql=require('mysql')constdb=mysql.createPool({host:'127.0.0.1......
  • Docker基础知识 (8) - 使用 Docker 部署 SpringBoot + MariaDB(MySQL)项目
    本文在“ Docker基础知识(7)-使用Docker部署SpringBoot项目”里的SpringbootWebDocker项目的基础上,添加JDBC、MariaDB和MyBatis相关依赖包和数据库操作代......
  • django中操作mysql数据库
    1.准备工作(django连接数据库)1.本机电脑下载好mysql数据库2.打开django,修改setting.py中的DATABASES配置项DATABASES={'default':{'ENGINE':'django.d......
  • MySQL数据库如何线上修改表结构
    一、MDL元数据锁在修改表结构之前,先来看下可能存在的问题。1、什么是MDL锁MySQL有一个把锁,叫做MDL元数据锁,当对表修改的时候,会自动给表加上这把锁,也就是不需要自己显式......
  • Windows10系统MySQL5.7升级到8.0
    转自:https://www.freesion.com/article/8785691339/记录MySQL5.7升级到8.0,卸载安装等解决方案1.停止MySQL服务2.卸载MySQL相关的程序step1:本次是win10系统环境;......
  • java mysql截取所需数据
    mysql截取数据:例:截取门铺名称,门铺名称长度不确定{"进店日期":"2022-09-01","电话":"1********25","姓名":"张三","单号":"90817","门铺":"吴滨路店","消费金额":"......
  • java mysql删除表中多余的重复记录(多个字段),只留有id最小的记录
    mysql删除表中多余的重复记录(多个字段),只留有id最小的记录DELETEFROM表1fWHERE(f.字段1,f.字段2)IN(SELECT字段1,字段2FROM表1GROUPBY字段1,字段2HAVING......
  • MySQL教程 - 事务(Transaction)
    更新记录转载请注明出处。2022年9月3日发布。2022年9月3日从笔记迁移到博客。事务说明事务(transaction)一种机制,用于执行成批的MySQL操作用以保证没有不完整的操......
  • MySQL教程 - 视图(View)
    更新记录转载请注明出处。2022年9月3日发布。2022年9月3日从笔记迁移到博客。说明视图是虚拟的表,是一种存储结构可以对视图进行和表一样的操作,但一般用于查询数......
  • MySQL教程 - 内建函数(Function)
    更新记录转载请注明出处。2022年9月3日发布。2022年9月3日从笔记迁移到博客。内建函数主要的函数类型数学函数字符串函数日期和时间函数条件判断函数系统信......