Mysql基础-数据的查询
数据查询是数据库使用过程中用到最多的,数据查询可用于单表查询,也可用于多表查询。
1.准备数据
-- 首先创建一个库test,使用utf8编码
create database test charset=utf8;-- 查看库是否创建成功
show databases;-- 使用库
use test;-- 查看在使用的库
select database();-- 创建一个表单students
create table students (id int unsigned primary key not null auto_increment,name varchar(20) not null,gender enum("男","女","保密") default "保密",age tinyint unsigned default 18,is_delete bit default 0
);-- 查看表单是否创建成功
show tables;-- 查看表单创建代码
show create table students;-- 创建另一个表单classes
create table classes(id int unsigned primary key not null auto_increment,name varchar(30) not null
);-- 导入student数据
insert into students(name,gender,age)values("张三",1,18),("林梦",2,19),("田甜",2,20),("李四",1,16),("王五",1,19),("韩婷",2,18),("赵六",1,19),("李洛璃",2,22),("肖八",1,19);-- 查看数据是否导入,数据多不建议全部查看
select * from students;-- 导入class数据
insert into classes(name) values
("高一一班"),("高二二班"),("高三三班");-- 突然发现第一个表没班级,新加班级字段
alter table students add cls_id int unsigned default 0;-- 更新班级信息
update students set cls_id=1 where age<=18;
update students set cls_id=2 where age=19;
update students set cls_id=3 where age>19;-- 数据导入完成,删除库可用 drop database test;
2.简单查询
-- 查询所有字段
select * from students;
select * from classes;
-- 查询指定字段
select id,name,gender from students;
select students.id,students.name from students;
-- 使用as给字段起别名
select id as 序号,name as 姓名,gender as 性别
from students;
-- 使用as给表起别名,不能使用原表名
select s.id,s.name from students as s;
-- 消除重复行 distinct
select distinct gender from students;
3.条件查询
3.1比较运算符
-- 查询大于18岁的信息 >
select * from students where age>18;
-- 查询小于18岁的信息 <
select * from students where age<18;
-- 查询等于18岁的信息 =
select * from students where age=18;
-- 查询不等于18岁的信息 != , <>
select * from students where age!=18;
3.2逻辑运算符
-- 查询18到19岁学生的信息 and
select * from students where age>=18 and age<=19;
-- 查询18岁以上或者女性 or
select * from students where age>=18 or gender=2;
-- 查询查询不是 18岁以上女性 not
select * from students where not (age>18 and gender=2);
3.3模糊查询
-- like 匹配字符,效率较低
-- _ 替代一个位置
-- % 替代剩余位置
-- 查询姓李的同学
select name from students where name like "李%";
-- 查询名字两个字以上的同学
select name from students where name like "___%";-- rlike 正则匹配
-- ^ 开头 .* 填充 $结尾
-- 查询姓韩的同学
select name from students where name rlike "^韩.*";
-- 查询以李开头 以璃结尾的同学
select name from students where name rlike "^李.*璃$";
3.4范围查询
-- in/not in (1,5,8) 在/不在一个非连续范围
-- 查看年龄为18和22岁的同学信息
select * from students where age in (18,22);
-- 查看年龄不为18和22岁的同学信息,not in是一个整体
select * from students where age not in (18,22);-- between/not between ... and ... 在/不在一个连续范围
-- 查看年龄为18到20岁的同学信息
select * from students where age between 18 and 20;
-- 查看年龄不在18到20岁的同学信息,not between是一个整体
select * from students where age not between 18 and 20;-- is/is not null 判断为空/不为空
-- 插入一个为空的数据
insert into students(name,gender,age,cls_id)values("张三三",2,18,null);
-- 查看班级为空的同学
select * from students where cls_id is null;
-- 查看班级不为空的同学
select * from students where cls_id is not null;
4.排序
-- order by `字段` 不写默认按主键排序
-- asc 从小到大排(默认值)
-- desc 从大到小排
-- 查询姓李的同学,年龄从大到小排
select * from students where name rlike "^李.*" order by age desc;-- order by 后面可跟多字段,依照顺序判断
-- 查询所有同学,班级从小到大排,年龄从小到大排
select * from students order by cls_id,age;
5.聚合分组
聚合函数 | 函数作用 |
---|---|
count(字段 )
|
总数 |
max(字段 )
|
最大值 |
min(字段 )
|
最小值 |
sum(字段 )
|
求和 |
avg(字段 )sum( 字段 )/count(字段 )
|
平均值 |
round(需保留的值 ,保留小数位数 )
|
四舍五入 |
-- 聚合函数
-- ps:计算机计算小数存在误差,当需要准确计算时可扩大相应倍数后计算
-- 计算平均年龄,保留两位小数
select round(avg(age),2) as 平均年龄 from students;
select round(sum(age)/count(age),2) as 平均年龄 from students;
-- 输出女性总数,最大年龄,最小年龄
select count(*) as 总数,max(age) as 最大年龄,
min(age) as 最小年龄 from students where gender=2;-- 输出女性所有名字,以及总数
select name,count(*) from students where gender=2;
-- 语句报错,需要分组后再进行计算-- group by 分组
-- 按照性别分组,select后跟非分组字段无意义,可跟聚合函数
select gender from students group by gender;
-- 按照性别分组并计算人数
select gender,count(*) from students group by gender;
-- group_concat(`字段`) 查询同组的所有字段
-- 查询每个性别里所有人的姓名,年龄
select gender,group_concat(name," ",age) from students group by gender;
-- having 对组进行约束,一般处理生成表单采用having,原表单使用where
-- 输出大于两个人的班级的所有人姓名
select cls_id,group_concat(name) from students group by cls_id having count(*)>2;-- 此时解决上面未解决的问题
-- 输出女性所有名字,以及总数
select group_concat(name),count(*) from students where gender=2 group by gender;
select group_concat(name),count(*) from students group by gender having gender=2;
6.分页
-- limit 在所有约束之后
-- limit `个数` 限制查询个数
-- 查询最开始的5个数据
select * from students limit 5;
-- limit `起始位置`,`个数` 查询从固定位置开始的几个记录
select * from students limit 0,5; #从1开始五个数据
select * from students limit 5,5; #从6开始五个数据
7.连接查询
SQL支持三种类型的连接查询:内连接,右连接,左连接,可实现如下图所示的多种效果。
-- inner join 内连接
-- 将学生信息和班级对应起来,as可省略
select * from students as s inner join classes con s.cls_id=c.id;-- left join 左连接
-- 右侧添加信息到左侧表单,没有对应填null
select * from students s left join classes con s.cls_id=c.id; -- right join 右连接,效果等同左连接反转
-- full join 全连接,可用左连接+约束条件实现-- 显示班级,编号,学生,性别,先按班级排序,再按编号排序
select c.name 班级,s.id 编号,s.name 学生,s.gender 性别 from students s inner join classes c on s.cls_id=c.idorder by c.id,s.id;
8.自关联
要实现三级城市选择,按照一般方法需要建最少三个表,由于表结构基本相同,因此可以使用一个表来完成,一个表内一个字段p_id关联另一字段id则称为自关联,如下表。
id | name | p_id |
---|---|---|
1 | 河北省 | null |
2 | 石家庄市 | 1 |
3 | 正定 | 2 |
[提取码:z04f ]:https://pan.baidu.com/s/1HAN9WwO4T-aQyVPf0-RBpQ 点击下载
8.1数据导入
```sql
-- 从上方链接下载完sql文件,解压选择单文件放在桌面上
-- 使用test库
use test
-- 导入sql文件
source C:\Users\Administrator\Desktop\cn_region_info.sql
-- 显示创建的表的字段信息
show create table cn_region_info;
-- 删掉不需要的字段
alter table cn_region_info drop column CRI_GMT_CREATE,drop column CRI_GMT_MODIFIED,drop column CRI_MEMO,drop column CRI_DATA_STATE,drop column CRI_TENANT_CODE;
-- 查看前6行信息
select * from cn_region_info limit 6;
-- 显示信息则导入成功
```
CRI_ID | CRI_CODE | CRI_NAME | CRI_SHORT_NAME | CRI_SUPERIOR_CODE | CRI_LNG | CRI_LAT | CRI_LEVEL |
---|---|---|---|---|---|---|---|
1 | 110000 | 北京 | 北京 | 000000 | 116.405289 | 39.904987 | 1 |
2 | 120000 | 天津 | 天津 | 000000 | 117.190186 | 39.125595 | 1 |
3 | 130000 | 河北省 | 河北 | 000000 | 114.502464 | 38.045475 | 1 |
4 | 140000 | 山西省 | 山西 | 000000 | 112.549248 | 37.857014 | 1 |
5 | 150000 | 内蒙古自治区 | 内蒙古 | 000000 | 111.670799 | 40.81831 | 1 |
6 | 210000 | 辽宁省 | 辽宁 | 000000 | 123.429092 | 41.796768 | 1 |
8.2数据查询
-- 查询河北省的代码130000
select cri_code from cn_region_info where cri_name="河北省";
-- 查询河北省下所有市
select * from cn_region_info where cri_superior_code=130000;
-- 查询衡水市的代码131100
select cri_code from cn_region_info where cri_superior_code=130000 and cri_name="衡水市";
-- 查询衡水市下所有县
select * from cn_region_info where cri_superior_code=131100;
-- 查询衡水市下所有县,显示市,县,一张表当成两个表使用连接
select s.cri_name 市,c.cri_name 县 from cn_region_info s inner join cn_region_info c onc.cri_superior_code=s.cri_code where s.cri_name="衡水市";
9.子查询
-- 一个select里嵌套一个select称为子查询
-- 查询衡水市下所有县
select * from cn_region_info where cri_superior_code=(select cri_code from cn_region_info where cri_superior_code=130000 and cri_name="衡水市");
-- 查询一个结果有多种方式,但所用时间不同
10.数据库设计
10.1 三范式
经过研究和对使用中问题的总结,对于设计数据库提出了一些规范,这些规范被称为范式(Normal Form),一般需要遵守三条即可。
第一范式(1NF)
:强调的是列的原子性,即***列不能够再分成其他列***。第二范式(2NF)
:首先是1NF,另外包含两部分内容,一是表必须有一个主键;二是没有包含在主键中的列必须完全依赖,而不能只是依赖于主键的一部分。第三范式(3NF)
:首先是2NF,另外非主键列必须直接依赖于主键,不能存在传递依赖。即***不能存在***:非主键列A依赖于非主键列B,非主键列B依赖于主键的情况。
PS:可适当降低标准,设计数据库是需求>性能>表结构,不能一味追求范式。
10.2 E-R模型
描述事物之间一对一,一对多,多对多的关系,当为一对一和一对多时,两个表即可解决问题,当为多对多时***需要添加一个中间表来关联两个表*。
Mysql基础-数据的查询相关推荐
- MySQL:基础—数据分组
MySQL:基础-数据分组 1.为什么要分组: 比如一个表中有多条订单记录,如上图,每条记录对应着一个商品,现在我要查询 每个商品被订购的单数 准备出货?也就是找到每个商品被订购的数量. 如果只找一个 ...
- MySQL基础_模糊查询—between and in is null <=>(安全等于)等关建字
文章目录 MySQL基础_模糊查询-in关键字 MySQL基础_模糊查询-is null关键字 MySQL基础_[补充]安全等于的介绍 eg: select * from table where 字段 ...
- 简单介绍MySQL插入数据与查询数据
这篇文章主要介绍了 MySQL插入数据与查询数据,缺省插入.缺省插入.缺省插入等各种数据插入分享,需要的小伙伴可以参考一下,希望对你有所帮助 一.插入数据 insert into 表名 (列1,... ...
- 【MySQL基础知识】查询、过滤数据关键字
MySQL基础知识 一.检索数据 1. SELECT select是使用最广泛的检索数据的语句. 检索要查的表的所有列: select * from (表名称)... 检索要查的表的某一列或多列: s ...
- MySQL 基础———— UNION 联合查询
引言 联合查询与连接查询不同,通过UNION 关键字,我们可以将多个查询语句一同执行并将结果集展示出来,不涉及到任何关联关系. UNION 的含义是"联合,并集,结合",在MySQ ...
- mysql基础操作和查询语句
MySQL基础 mysql基础操作操作 MySQL基础 相关术语 数据库操作 MySQL管理 启动及关闭 MySQL 用户设置 /etc/my.cnf 文件配置 管理MySQL的命令 连接MySQL ...
- MySQL 大数据in查询该怎么优化
对于 MySQL 中的大数据 IN 查询,可以使用以下方法进行优化: 尽可能使用索引.如果使用 IN 操作符的字段有索引,MySQL 将会使用索引来加速查询. 尽量使用常量作为 IN 操作符中的参数, ...
- MySql基础教程(三)——查询训练
在MySql两轮基础的学习之后,来一波实战演习... 三张表:学生表,课程表,成绩表. 建表详细信息见 MySql基础教程(一) 转载于:https://www.cnblogs.com/zhouerb ...
- 【mysql基础知识】查询当前时间之前5分钟内的数据
通过sql语句查询与当前时间相隔5分钟以内的数据. select * from 数据表名 where 字段名 between date_add(now(), interval - 5 minute) ...
最新文章
- Windows phone应从Android吸取四点教训
- 五十八、如何对一个数进行分解质因数
- 外网利用POP3,SMTP访问内网Exchange!!!
- bootstrap table入门例子
- QT设置相对路径最简单方法
- Midi 乐器set
- 20.并发容器之ArrayBlockingQueue和LinkedBlockingQueue实现原理详解
- 【WordCloud】将周董的歌用词云可视化
- matlab covar,Matlab功率谱估计
- 应聘软件测试岗位需要掌握的基础知识与技能(面试常考内容)
- Excel设置(单行或多行)固定表头的方法
- [转]中国青岛发布新一代CPU桥片“星光青桥一号”
- python中用于进行web开发的第三方库是_10款Web开发最佳的Python框架
- 麓言科技CAD制图技巧
- 小学计算机教师教育教学故事,小学教师教育叙事故事
- Code39码如何批量生成
- codeforces #309 div2
- android 字母索引 listview
- 全球与中国地下用钢纤维市场深度研究分析报告
- 车载调频广播电台移动发射系统应用
热门文章
- 2019ICPC女生专场比赛心得体会
- 中国人民银行公布人民币外汇货币掉期业务规范
- 【数据结构与算法】二叉树深度遍历
- springboot全局异常处理详解
- 【逗老师的小技巧】树莓派4翻转屏幕,加载OpenGL驱动
- 字符串缓冲区StringBuffer类,正则表达式
- 关于run time error的处理
- 19年 招行暑期实习笔试题有感!!
- python pygame鼠标点击_python – 如何在PyGame中响应鼠标点击sprite?
- android自定义camera预览区域,android camera摄像surfaceview预览界面特定区域(该区域可移动)...