7.MySQL单表查询
文章目录
- 1. 复制表
- 2. 分行展示
- 3. 查询关键字
- 4. 模糊查询
- 5. 常用运算符
- 5.1数学运算符
- 5.2比较运算符
- 5.3 逻辑运算符号
- 5.4 成员运算
- 5.5 范围运算符
- 6.where 筛选练习
- 6.1模板
- 6.2 题
- 7. grop by 分组
- 7.1 严格模式
- 7.2 group_concat 函数
- 7.3 聚合函数
- 7.4 注意事项
- 7.5分组练习
- 8. 点的作用
- 9. as 起别名
- 10. having 过滤
- 12. distinct 去重
- 12. order by 排序
1. 复制表
sql语句查询的结果可以看成是一张虚拟表.
可以创建一张表 把 查询表的结果写入其中.
create table 创建新表名 select * from 复制的表名;
注意点:
1. 不能复制主键,外键,索引,只能复制表结构,数据。
2. 查询表的结果为空,那么复制的就是一张空表.
# 如果复制一张表,表中只有一条数据值id为1,
# 表设置了条件 id>30 没有满足条件的数据,那么复制的就是一张空表。
create table new_t1 select * from t1 where id > 30;
2. 分行展示
当表字段特别多可以使用\G分行展示。
select * from emp\G;
3. 查询关键字
from 控制的是查询那张表
select 控制的是查询表中的字段
where 对整体数据的筛选操作。
书写顺序:
select id ,name from emp where id > 3;
执行顺序:
from where select
书写技巧:按书写顺序的方式写sql语句
select * 先用*占位,之后补全后面的sql语句,在后将*号替换想要的具体内容。
4. 模糊查询
当没有明确的筛选条件使用.
模糊查询:like
% : 匹配任意多个字符,(匹配字符可为空)
_ : 匹配任意单个字符。(匹配字符不可为空)
5. 常用运算符
5.1数学运算符
+ - * / %
加 减 乘 除 取余
5.2比较运算符
> < = >= <=
大于 小于 等于 大于等于 小于等于
5.3 逻辑运算符号
and or not与 或 非
5.4 成员运算
in 判断某个值时候在某个群体里面
is 判断值是否相等,可以用于判断值是否为null;
5.5 范围运算符
between x1 and x2 在什么之间
6.where 筛选练习
6.1模板
# 创建表
create table emp(id int not null unique auto_increment,name varchar(20) not null,sex enum('male', 'female') not null default 'male',age int(3) unsigned not null default 28,hire_date date not null,post varchar(50),post_comment varchar(100),salary double(15,2),office int,depart_id int
);
# 插入数据 三个部门:教学,销售,运营
insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values
('盘古','male',78,'20150302','teacher',1000000.31,401,1),
('帝江','male',81,'20130305','teacher',8300,401,1),
('后羿','male',73,'20140701','teacher',3500,401,1),
('罗睺','male',28,'20121101','teacher',2100,401,1),
('扬眉','female',18,'20110211','teacher',9000,401,1),
('共工','male',18,'19000301','teacher',30000,401,1),
('鸿钧','male',48,'20101111','teacher',10000,401,1),
('帝俊','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
('太一','female',38,'20101101','sale',2000.35,402,2),
('通天','female',18,'20110312','sale',1000.37,402,2),
('红云','female',18,'20160513','sale',3000.29,402,2),
('祖龙','female',28,'20170127','sale',4000.33,402,2),
('始麒麟','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
('元凤','male',18,'19970312','operation',20000,403,3),
('神逆','female',18,'20130311','operation',19000,403,3),
('女娲','male',18,'20150411','operation',18000,403,3),
('后土','female',18,'20140512','operation',17000,403,3);
6.2 题
1.查询id大于等于 3 小于等于 6 的数据。
select * from emp where id>=3 and id <=6;
select id, name, age from emp where id>=3 and id <=6;
select id, name, age from emp where id between 3 and 6; # 两者等价 between 之间的
2.查询薪资是20000或者18000或者17000的数据。
select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
select * from enp where salary in (2000,18000,17000);
3.查询薪资不在2000,18000,17000范围的数据。
select * from emp where salary not in (20000,18000,17000);
4.查询员工姓名中包含’后‘的员工的姓名和薪资。
select name, salary from emp where name like '%后%';
# 两个百分号,'后' 的前面可以出现任意个字符,后面也可以出现任意个字符。
5.查询员工姓名是由三个字符组成的, 将他的姓名薪资显示出来。
select name, salary from emp where name like '___'; # 模糊查询三个_
select name, salary from emp where char_length(name) = 3; # char_length() 获取字符的长度。
6.查询id小于3 或者 id大于6 的数据 。
select * from emp where id not between 3 and 6;
7.查询岗位描述为空的员工姓名和岗位。
select name, post from emp where post_comment=NULL; # 不能用等号 使用 is
select name, post from emp where post_comment is NULL;
select name, post from emp where char_length(post_comment)=0; # 此法不行
8.查询岗位描述为非空的员工姓名和岗位。(表中没有这个,需要添加数据)
insert into emp(name,sex,age,hire_date,post,post_comment,salary,office,depart_id) values
('我是kid','male',78,'20150302','teacher','我在学习',1000000.31,401,1);
select name, post from emp where not post_comment is NULL;
select name, post from emp where post_comment is not NULL;
9 .查询每个人的姓名和年薪
select name,salary * 12 from emp;
7. grop by 分组
分组:按照某个指定的条件将单个单个的个体,合成一个个整体.
分组之后的好处是方便管理.
grop by 字段名; 表格按自定字段进行分组。
select * from emp group by post;
# 拿到每个分组的第一条信息,如果设置了严格模式,那命令直接报错。
# 分组之后最小的操作单位是组,而不是是组内的单个数据。
7.1 严格模式
上面这个命令在没设置严格模式的时候是可以正常执行的,返回的分组之后每个组的第一条数据,
但是这不符合分组的规范,分组之后不应该考虑单个数据,而因该以组为操作单位,
分组之后,没办法直接获取组内单个数据。上面这个命令或报错.这就需要设置严格模式.
在严格模式中加上 'only_full_group_by' 仅分组字段才能查询.
# 这个语句是覆盖操作,之前设置的严格模式不加上去的话就只会剩下一个.
# 严格模式设置, 设置好了之后,退出再登入。
set global sql_mode = 'strict_trans_tables,only_full_group_by';
# 错误操作 only前加了个空格。
set global sql_mode = 'strict_trans_tables, only_full_group_by';
show variables like '%mode%';
+----------------------------+----------------------------------------+
| Variable_name | Value |
+----------------------------+----------------------------------------+
| binlogging_impossible_mode | IGNORE_ERROR |
| block_encryption_mode | aes-128-ecb |
| gtid_mode | OFF |
| innodb_autoinc_lock_mode | 1 |
| innodb_strict_mode | OFF |
| pseudo_slave_mode | OFF |
| slave_exec_mode | STRICT |
| sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES |← 查看
+----------------------------+----------------------------------------+
use db1;
select * from emp group by post;
# ERROR 1055 (42000): 'db1.emp.id' isn't in GROUP BY
设置严格模式之后 分组默认只能拿到分组的依据,按照什么分组就只能拿到什么。
group by 后面跟着什么 select 后面就只能跟什么.
其他字段不能直接获取需要借助一些方法。
select name from emp group by post;
# ERROR 1055 (42000): 'db1.emp.name' isn't in GROUP BY 字段name 不在分组中
select post from emp group by post;
select post from emp group by post;
+-----------+
| post |
+-----------+
| operation |
| sale |
| teacher |
+-----------+
7.2 group_concat 函数
group_concat(字段名)
1.获取分组的其他字段,可以是多个字段.查询的结果汇总成是一条数据.
2.拼接操作。
7.3 聚合函数
聚合函数只能用于分组后的操作.
max() 最大值
min() 最小值
sum() 求和
count() 统计 * 无法计算null的数量
avg () 平均值
分组之后可以在聚合函数内放入其他的字段.
按字段名分组,字段中值相同的被视为一组,每一组可以看成是一个单独的表.
格式:
selec 分组的依据字段,聚合函数(其他字段) from 表名 条件;
7.4 注意事项
关键字where 与 group by 同时出现的时候必须在where的后面,
where 先对整体数据进行过滤之后再分组操作。
where 函数后面不能跟聚合函数。
select id,name from emp where max(salary) > 3000;
# Invalid use of group function 非法使用集合函数
7.5分组练习
0.统计各部门年龄在30岁以上的员工平均薪资
# 1.先求所有年龄大于30岁以上的员工
select * from emp where age>30;# 2.在对结果进行分组
select * from emp where age>30 group by post;# 3.完整的语句
select post,avg(salary) from emp where age>30 group by post;
1.获取每个部门的最高薪资。
select post,max(salary) from emp group by post;
select post as '部分',max(salary) as '最高薪资' from emp group by post;
+-----------+--------------+
| 部门 | 最高薪资 |
+-----------+--------------+
| operation | 20000.00 |
| sale | 4000.33 |
| teacher | 1000000.31 |
+-----------+--------------+
2.获取每个部门的最低薪资。
select post ,min(salary) from emp group by post;
3.获取每个部门的平均薪资。
select post ,avg(salary) from emp group by post;
4.获取每个部门的综合薪资。
select post ,sum(salary) from emp group by post;
5.获取每个部门的人数。
select post ,count(id) from emp group by post; # 推荐统计唯一的主键
# count,无法计算null的数量。
select post ,count(post_comment) from emp group by post;
+-----------+---------------------+
| post | count(post_comment) |
+-----------+---------------------+
| operation | 0 |
| sale | 0 |
| teacher | 0 |
+-----------+---------------------+
6.分组之后的部门名称和每个部门下所有的员工姓名。
select post ,group_concat(name) from emp group by post;
7.名称的拼接。--> 添加前缀/后缀
select post ,group_concat(name,'——洪荒大世界') from emp group by post; # 后缀
select post ,group_concat('洪荒大世界——',name) from emp group by post; # 前缀
8.获取多个字段
分组之后的部门名称和每个部门下所有的员工姓名和薪资.
select post ,group_concat(name,salary) from emp group by post;
9.多个字段之间添加分隔符号。
select post ,group_concat(name,':',salary) from emp group by post;
10.concat单独使用是拼接。前缀,后缀.
select concat('洪荒大世界:', name),concat('混沌币:',salary) from emp;
8. 点的作用
点 . 在SQL可以最为连接符(库名.表名.字段)
点(.)之前的表示一个对象点(.)后的表示该对象的属性.
# 不进入库直接创建表
create table db1.t1(id int);
# Query OK, 0 rows affected (0.02 sec)# 不进入库直接查询表
select * from db1.t1;
# Empty set (0.00 sec)
9. as 起别名
as 语法可以给字段,表起别名.
# 给字段起别名
select post as '部分',max(salary) as '最高薪资' from emp group by post;
在给表起别名的时候需要注意,表名修改后,sql语句中就不能在出现原来的表名了.
# 错误写法,表名临时性被改为t1 emp不能在出现
select emp.id,emp.name from emp as t1;# 正确写法
select t1.id,t1.name from emp as t1;
select id,name from emp as t1;
10. having 过滤
having 在分组之前进行过滤操作,可以直接使用聚合函数。
where 在分组之前进行过滤操作,无法使用聚合函数.
统计各部门在30岁以上的员工,并且保留平均工资大于10,000的部门。
# 1.先使用where 过滤出age>30的员工.
select * from emp where age >30;
# 2.在对部门进行分组
select post from emp where age > 30 group by post;
# 3.在求每个部门的平均工资
select postfrom emp where age > 30 group by post having avg(salary);
# 4.过滤出平均工资大于10000的部门名称
select post from emp where age > 30 group by post having avg(salary) > 10000;
12. distinct 去重
distinct 去重
去重的前提条件:数据必须一模一样。
distinct必须紧跟着select的后面.
select显示的字段只能是distinct指定的字段,其他字段是不可能出现的。
# 单字段去重: select distinct age from emp;
# 多字段去重: select distinct agr,post from emp;
* 如果在数据中有主键,就没有必要做什么去重了。
select distinct id,post from emp;
12. order by 排序
order by 字段 asc/desc; 以xx字段排序.
数字按大小排序.英文字符按字母a-z排序表排序.
中文的比较复杂,baidu去吧我就不弄了.
# 升序asc(两者等价,建议写出asc)
select * from emp order by salary;
select * from emp order by salary asc;# 降序 desc
select * from emp order by salary desc;
# 排序可以指定多个字段排序.第一个字段相同的情况下,排序第二个字段。
select * from emp order by post asc, salary desc;
# 首先满足第一个条件name必须有同,不然后面不会执行了。例下sql则失效.
select * from emp order by name asc, salary desc;
7.MySQL单表查询相关推荐
- Mysql 单表查询where初识
Mysql 单表查询where初识 准备数据 -- 创建测试库 -- drop database if exists student_db; create database student_db ch ...
- 运维高级学习(三):MySQL单表查询作业
MySQL第三次作业 MySQL单表查询作业 素材如下: DROP TABLE IF EXISTS `course`; CREATE TABLE `course` ( `cs_id` int(11) ...
- MySQL单表查询基础卷(A)
MySQL单表查询基础卷(A) 第一章 数据准备 第二章 数据展示 第三章 题目说明 第四章 参考答案 第一章 数据准备 -- 创建数据库,指定字符集 utf8 CREATE DATABASE IF ...
- mysql 单表查询
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名WHERE 条件GROUP BY fieldHAVING 筛选ORDER BY fieldLIMIT 限制条数 二 关键字的执行优 ...
- 0x06 MySQL 单表查询
一 单表查询语法 SELECT 字段1,字段2... FROM 表名WHERE 条件GROUP BY fieldHAVING 筛选ORDER BY fieldLIMIT 限制条数 二 关键字执行优先级 ...
- mysql单表查询详解
文章目录 一.单表查询 1.1 创建数据库 1.2 单表查询 1.2.1 查询所有字段 1.2.2 使用通配符*查询所有字段 1.2.3 查询指定字段 1.2.4 去重distinct的使用 1.2. ...
- mysql单表查询实例_MySQL简单查询详解-单表查询
MySQL简单查询详解-单表查询 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查询的执行路径 一条SQL查询语句的执行过程大致如下图所示: 1>.客户端和服务端通过my ...
- Mysql单表查询和多表查询
单表查询 一 单表查询的语法 #查询数据的本质:mysql会到你本地的硬盘上找到对应的文件,然后打开文件,按照你的查询条件来找出你需要的数据.下面是完整的一个单表查询的语法select * from, ...
- MySQL单表查询练习题
数据准备:install.bat @ECHO OFF ::未配置环境变量下 cd C:\Program Files\MySQL\MySQL Server 5.7\bin\ SET dbhost=127 ...
- MySQL单表查询例题
一.单表查询 CREATE TABLE `worker` ( `部门号` int(11) NOT NULL, `职工号` int(11) NOT NULL, `工作时间` date NOT NU ...
最新文章
- 初学jQuery之jQuery虚假购物车-------与真实数据无关
- 邓迎春绘画201702作品08
- 什么是以太网光纤收发器,其产品特点和技术参数都有哪些?
- 1420C1. Pokémon Army (easy version)
- div中iframe高度自适应问题
- Python-类与文件读取结合
- java RuntimeException
- Linux调出git页面,Linux 显示 git 分支 及 完整路径
- springboot统一异常处理及返回数据的处理
- 开启Accessibility的快捷方式-3次home键或者侧边键
- Android PDF文件阅读方案
- 电力电子技术各类整流电路Matlab_simulink仿真分析
- [转]Maven查看当前生效配置、pom、环境变量等命令(mvn help用法)
- 1024Studio官网
- 如何将图片制作成画中画特效
- Vultr 修改 Root 账户密码教程
- ffmpeg 音频解码一
- 【Python_绘图】折线图与散点图
- Mybatis-Plus的条件构造器(Wrapper)
- 网上计算机能力提升研修心得,信息技术应用能力提升个人研修总结