阅读目录

一,查询语法

二,简单查询

三,where约束

四,having过滤

五,分组查询 group by

六,关键字的执行优先级

七,查询排列 order by

八,使用聚合函数查询

九,where补充

十,限制查询的记录数:limit

========================================================================================================================================

先创建表

  1. #创建表
  2. create table employee(
  3. id int not null unique auto_increment,
  4. name varchar(20) not null,
  5. sex enum('male','female') not null default 'male', #大部分是男的
  6. age int(3) unsigned not null default 28,
  7. hire_date date not null,
  8. post varchar(50),
  9. post_comment varchar(100),
  10. salary double(15,2),
  11. office int, #一个部门一个屋子
  12. depart_id int
  13. );
  14.  
  15. #查看表结构
  16. mysql> desc employee;
  17. +--------------+-----------------------+------+-----+---------+----------------+
  18. | Field | Type | Null | Key | Default | Extra |
  19. +--------------+-----------------------+------+-----+---------+----------------+
  20. | id | int(11) | NO | PRI | NULL | auto_increment |
  21. | name | varchar(20) | NO | | NULL | |
  22. | sex | enum('male','female') | NO | | male | |
  23. | age | int(3) unsigned | NO | | 28 | |
  24. | hire_date | date | NO | | NULL | |
  25. | post | varchar(50) | YES | | NULL | |
  26. | post_comment | varchar(100) | YES | | NULL | |
  27. | salary | double(15,2) | YES | | NULL | |
  28. | office | int(11) | YES | | NULL | |
  29. | depart_id | int(11) | YES | | NULL | |
  30. +--------------+-----------------------+------+-----+---------+----------------+
  31.  
  32. #插入记录
  33. #三个部门:教学,销售,运营
  34. insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
  35. ('egon','male',18,'','teacher',7300.33,401,1), #以下是教学部
  36. ('alex','male',78,'','teacher',1000000.31,401,1),
  37. ('wupeiqi','male',81,'','teacher',8300,401,1),
  38. ('yuanhao','male',73,'','teacher',3500,401,1),
  39. ('liwenzhou','male',28,'','teacher',2100,401,1),
  40. ('jingliyang','female',18,'','teacher',9000,401,1),
  41. ('jinxin','male',18,'','teacher',30000,401,1),
  42. ('成龙','male',48,'','teacher',10000,401,1),
  43.  
  44. ('歪歪','female',48,'','sale',3000.13,402,2),#以下是销售部门
  45. ('丫丫','female',38,'','sale',2000.35,402,2),
  46. ('丁丁','female',18,'','sale',1000.37,402,2),
  47. ('星星','female',18,'','sale',3000.29,402,2),
  48. ('格格','female',28,'','sale',4000.33,402,2),
  49.  
  50. ('张野','male',28,'','operation',10000.13,403,3), #以下是运营部门
  51. ('程咬金','male',18,'','operation',20000,403,3),
  52. ('程咬银','female',18,'','operation',19000,403,3),
  53. ('程咬铜','male',18,'','operation',18000,403,3),
  54. ('程咬铁','female',18,'','operation',17000,403,3)
  55. ;
  1. 1.注意:
  2. select * from t1 where 条件 group by 分组字段
  3. 1.分组只能查询分组字段,要想查看其余的利用聚合函数
  4. 2.聚合函数的分类:count,min,max,avg,group_concat,sum等。
  5. 3.模糊匹配:用like关键字。
  6. select * from t1 where name like '%eg%'; #%表示任意字符
  7. select * from t1 where name like 'd__l'; #一个下划线表示一个字符,两个下划线就表示两个字符
  8. 4.拷贝表 create table t2 select * from t1;
  9. create table t2 select * from t1 where 1=2 ;

知识点回顾

一.查询语法

回到顶部

  1. SELECT 字段1,字段2... FROM 表名
  2. WHERE 条件
  3. GROUP BY field
  4. HAVING 筛选
  5. ORDER BY field
  6. LIMIT 限制条数

二.简单查询

回到顶部

  1. #简单查询
  2. SELECT id,name,sex,age,hire_date,post,post_comment,salary,office,depart_id
  3. FROM employee;
  4.  
  5. SELECT * FROM employee;
  6.  
  7. SELECT name,salary FROM employee;
  8.  
  9. #避免重复distinct
  10. SELECT DISTINCT post FROM employee;
  11.  
  12. #通过四则运算查询
  13. SELECT name, salary*12 FROM employee;
  14. SELECT name, salary*12 AS Annual_salary FROM employee;
  15. SELECT name, salary*12 Annual_salary FROM employee;
  16.  
  17. #定义显示格式
  18. concat() 函数用于连接字符串
  19. select concat('姓名: ',name,' 年薪: ', salary*12) AS Annual_salary
  20. FROM employee;
  21.  
  22. CONCAT_WS() 第一个参数为分隔符
  23. SELECT CONCAT_WS(':',name,salary*12) AS Annual_salary
  24. FROM employee;

小练习:

  1. 查出所有员工的名字,薪资,格式为
  2. <名字:egon> <薪资:3000>
  3. select concat('<名字:',name,'> ' ,'<薪资:',salary,'>' ) from employee;
  4. 查出所有的岗位(去掉重复)
  5. select distinct depart_id from employee;
  6. 查出所有员工名字,以及他们的年薪,年薪的字段名为年薪
  7. select name,salary*12 年薪 from employee;

三.where约束

回到顶部

where字句中可以使用:

1.比较运算符:>  <      >=      <=      <>      !=

2. between 80 and 100 值在10到20之间
3. in(80,90,100) 值是80或90或100
4. like 'eg%'
    可以是%或_,
    %表示任意多字符
    _表示一个字符

 like 'e__n' :
5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not

  1. #1:单条件查询
  2. SELECT name FROM employee
  3. WHERE post='sale';
  4.  
  5. #2:多条件查询
  6. SELECT name,salary FROM employee
  7. WHERE post='teacher' AND salary>10000;
  8.  
  9. #3:关键字BETWEEN AND
  10. SELECT name,salary FROM employee
  11. WHERE salary BETWEEN 10000 AND 20000;
  12.  
  13. SELECT name,salary FROM employee
  14. WHERE salary NOT BETWEEN 10000 AND 20000;
  15.  
  16. #4:关键字IS NULL(判断某个字段是否为NULL不能用等号,需要用IS)
  17. SELECT name,post_comment FROM employee
  18. WHERE post_comment IS NULL;
  19.  
  20. SELECT name,post_comment FROM employee
  21. WHERE post_comment IS NOT NULL;
  22.  
  23. SELECT name,post_comment FROM employee
  24. WHERE post_comment=''; 注意''是空字符串,不是null
  25. ps
  26. 执行
  27. update employee set post_comment='' where id=2;
  28. 再用上条查看,就会有结果了
  29.  
  30. #5:关键字IN集合查询
  31. SELECT name,salary FROM employee
  32. WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ;
  33.  
  34. SELECT name,salary FROM employee
  35. WHERE salary IN (3000,3500,4000,9000) ;
  36.  
  37. SELECT name,salary FROM employee
  38. WHERE salary NOT IN (3000,3500,4000,9000) ;
  39.  
  40. #6:关键字LIKE模糊查询
  41. 通配符’%’
  42. SELECT * FROM employee
  43. WHERE name LIKE 'eg%';
  44.  
  45. 通配符’_
  46. SELECT * FROM employee
  47. WHERE name LIKE 'al__';

四.having过滤

回到顶部

having 和 where语法上是一样的。但还是有区别(哈哈)

  1. select * from employee where id>15;
  2. select * from employee having id>15;

having和where在以下几点上有区别!!!

  1. #!!!执行优先级从高到低:where > group by > 聚合函数 > having >order by
  2. 1.wherehaving的区别
  3. 1. Where 是一个约束声明,使用Where约束来自数据库的数据,Where是在结果返回之前起作用的
  4. (先找到表,按照where的约束条件,从表(文件)中取出数据),Where中不能使用聚合函数
  5. 2.Having是一个过滤声明,是在查询返回结果集以后对查询结果进行的过滤操作
  6. (先找到表,按照where的约束条件,从表(文件)中取出数据,然后group by分组,
  7. 如果没有group by则所有记录整体为一组,然后执行聚合函数,然后使用having对聚合的结果进行过滤),
  8. Having中可以使用聚合函数。
  9. 3.where的优先级比having的优先级高
  10. 4.having可以放到group by之后,而where只能放到group by 之前。

验证不同之处:

  1. 1.查看员工的id>15的有多少个
  2. select count(id) from employee where id>15;#正确,分析:where先执行,后执行聚合count(id),
  3. 然后select出结果
  4. select count(id) from employee having id>15; #报错,分析:先执行聚合count(id),后执行having过滤,
  5. #无法对id进行id>15的过滤
  6. #以上两条sql的顺序是
  7. 1:找到表employee--->用where过滤---->没有分组则默认一组执行聚合count(id)--->select执行查看组内id数目
  8. 2:找到表employee--->没有分组则默认一组执行聚合count(id)---->having 基于上一步聚合的结果(此时只有count(id)字段了)
  9. 进行id>15的过滤,很明显,根本无法获取到id字段
  1. 1 ------having-----------
  2. 2 select depart_id,count(id) from employee group by depart_id;
  3. 3 select depart_id,count(id) from employee group by depart_id having depart_id = 3;
  4. 4 select depart_id,count(id) from employee group by depart_id having count(id)>7;
  5. 5 select max(salary) 最大工资 from employee where id>2 group by depart_id having count(id)>3;
  6. 6 select * from employee where id>7; #查看所有id>7的员工信息

having举例

五.group by 分组查询

回到顶部

大前提:可以按照任意字段分组,但分完组后,只能查看分组的那个字段,要想取的组内的其他字段信息,需要借助函数

  1. 单独使用group by关键字分组
  2. select post from employee group by post;
  3. 注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数
  4.  
  5. group by关键字和group_concat()函数一起使用
  6. select post,group_concat(name) from employee group by post;#按照岗位分组,并查看组内成员名
  7. select post,group_concat(name) as emp_members FROM employee group by post;
  8.  
  9. group by与聚合函数一起使用
  10. select post,count(id) as count from employee group by post;#按照岗位分组,并查看每个组有多少人

强调:

分组,一般相同的多的话就可以分成一组(一定是有重复的字段)

小练习:

  1. 1. 查询岗位名以及岗位包含的所有员工名字
  2. select post,group_concat(name) from employee group by post;
  3.  
  4. 2. 查询岗位名以及各岗位内包含的员工个数
  5. select post,count(id) from employee group by post;
  6.  
  7. 3. 查询公司内男员工和女员工的个数
  8. select sex,count(id) from employee group by sex;
  9.  
  10. 4. 查询岗位名以及各岗位的平均薪资
  11. select post,max(salary) from employee group by post;
  12.  
  13. 5. 查询岗位名以及各岗位的最高薪资
  14. select post,max(salary) from employee group by post;
  15.  
  16. 6. 查询岗位名以及各岗位的最低薪资
  17. select post,min(salary) from employee group by post;
  18.  
  19. 7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
  20. select sex,avg(salary) from employee group by sex;

六.关键字的执行优先级(重点)重点中的重点:关键字的执行优先级

回到顶部

  1. 1 from #找到表
  2.  
  3. 2 where  #拿着where指定的约束条件,去文件/表中取出一条记录
  4.  
  5. 3 group by  #将取出来的一条条记录进行分组group by,如果没有group by,则整体作为一组
  6.          如果有聚合函数,则将组进行聚合
  7. 4 having    #将 进行聚合的结果过滤
  8.  
  9. 5 select    #查出结果
  10.  
  11. 6 distinct    #去重
  12.  
  13. 7 order by    #将6的结果按照条件排序
  14.  
  15. 8 limit      #将7的结果限制显示行数

七.查询排序 order by

回到顶部

  1. 按单列排序
  2. select * from employee order by salary;
  3. select * from employee order by salary asc;
  4. select * from employee order by salary desc;
  5.  
  6. 按多列排序:先按照age排序,如果年级相同,则按照薪资排序
  7.  
  8. select * from employee order by age,salary desc;
  9.  
  10. ===========order by==========
  11. 1.select * from employee order by salary;#如果不指定,默认就是升序
  12. 2.select * from employee order by salary asc;
  13. 3.select * from employee order by salary desc;
  14.  
  15. #先按照年龄升序,当年龄相同的太多,分不清大小时,在按照工资降序
  16. 4.select * from employee order by age asc, salary desc;

小例子:

  1. 1. 查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序
  2. select * from employee order by age,hire_date desc;
  3.  
  4. 2. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列
  5. select post ,avg(salary) from employee group by post having avg(salary)>10000;
  6.  
  7. 3. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列
  8. select post ,avg(salary) from employee group by post having avg(salary)>10000 desc;

八.使用聚合函数查询

回到顶部

先from找到表

再用where的条件约束去表中取出记录

然后进行分组group by,没有分组则默认一组

然后进行聚合

最后select出结果

  1. 示例:
  2. select count(*) from employee;
  3. select count(*) from employee where depart_id=1;
  4. select max(salary) from employee;
  5. select min(salary) from employee;
  6. select avg(salary) from employee;
  7. select sum(salary) from employee;
  8. select sum(salary) form employee WHERE depart_id=3;

九.where的补充(使用正则表达式查询)

回到顶部

  1. 1.select * from employee where name regexp '^ale'; #匹配以ale开头的员工信息
  2. 2.select * from employee where name regexp 'on$'; #匹配以on结尾的员工信息
  3. 3.select * from employee where name regexp 'n{1,2}'; #匹配name里面包含1到2个n的员工信息
  4. 小结:对字符串匹配的方式
  5. where name = 'egon';
  6. where name like 'yua%';
  7. where name regexp 'on$';

小练习:

  1. 查看所有员工中名字是jin开头,n或者g结果的员工信息
  2. select * from employee where name regexp '^jin.*[ng]$';

十.限制查询的记录数:limit

回到顶部

  1. =========limit:限制打印几条=========
  2. 1.select * from employee limit 3;#打印前三条
  3. 2.像这样表示的:指的是从哪开始,往后取几条 (这样的操作一般用来分页)
  4. select * from employee limit 0,3;
  5. select * from employee limit 3,4;
  6. select * from employee limit 6,3;
  7. select * from employee limit 9,3;
  8. 3.select * from employee order by id desc limit 3; #查看后三条

小练习:

  1. 1. 分页显示,每页5
  2. select * from employee limit 0,5;
  3. select * from employee limit 5,5;
  4. select * from employee limit 10,5;

MySQL 单表查询(Day42)的更多相关文章

  1. Mysql 单表查询 子查询 关联查询

    数据准备: ## 学院表create table department( d_id int primary key auto_increment, d_name varchar(20) not nul ...

  2. python 3 mysql 单表查询

    python 3 mysql 单表查询 1.准备表 company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职 ...

  3. Mysql 单表查询-排序-分页-group by初识

    Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...

  4. Mysql 单表查询where初识

    Mysql 单表查询where初识 准备数据 -- 创建测试库 -- drop database if exists student_db; create database student_db ch ...

  5. MySQL单表查询

    MySQL之单表查询 创建表 # 创建表 mysql> create table company.employee5( id int primary key AUTO_INCREMENT not ...

  6. python mysql 单表查询 多表查询

    一.外键 变种: 三种关系: 多对一 站在左表的角度: (1)一个员工 能不能在 多个部门? 不成立 (2)多个员工 能不能在 一个部门? 成立 只要有一个条件成立:多 对 一或者是1对多 如果两个条 ...

  7. mysql 单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数   二 ...

  8. SQL学习笔记四(补充-1)之MySQL单表查询

    阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录 ...

  9. python开发mysql:单表查询&多表查询

    一 单表查询,以下是表内容 一 having 过滤 1.1 having和where select * from emp where id > 15; 解析过程;from > where ...

  10. 0x06 MySQL 单表查询

    一 单表查询语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键字 ...

随机推荐

  1. python的and和or优先级

    原题 输入一年份,判断该年份是否是闰年并输出结果.(编程题) 注:凡符合下面两个条件之一的年份是闰年. (1) 能被4整除但不能被100整除. (2) 能被400整除. 答案: def get_yea ...

  2. mybatis之高级结果映射

    先贴一句官方文档内容 如果世界总是这么简单就好了. 正如官方文档所说:如果一切都是这么简单,那该多好啊,但是实际上,我们面对的是复杂的对象,就是对象里有对象,有列表对象,总之五花八门的对象.这个时候我 ...

  3. jquery Fancybox使用教程

    Fancybox是一款基于jquery的对图片展示播放的插件,当然,它html文本.flash动画.iframe以及ajax也予以支持.还可以通过css自定义外观,阴影效果超级赞! 演示效果:http ...

  4. eclipse闪退问题

     昨日闲来无事,从eclipse官网下载了最新的eclipse版本,解压安装之后,便把之前安装的eclipse删除了,随后点击新安装的eclipse出现闪退问题,几经波折终于解决.        方法 ...

  5. 8、手把手教React Native实战之ReactJS组件生命周期

    1.创建阶段 getDefaultProps:处理props的默认值 在React.createClass调用 2.实例化阶段 React.render(<HelloMessage 启动之后 g ...

  6. Response响应对象

    1.HttpServletResponse HttpServletResponse是一个定义在Servlet API中的接口,继承自ServletReponse接口,用于封装HTTP响应消息.HTTP ...

  7. linux配置网关

    linux配置网关 输入账号root 再输入安装过程中设置的密码,登录到系统 vi /etc/sysconfig/network-scripts/ifcfg-eth0 #编辑配置文件,添加修改以下内容 ...

  8. Appcompat实现Action Bar的兼容性处理

    Appcompat实现Action Bar时,如果使用到split action bar或者Navigating Up with the App Icon需要考虑兼容性.下面介绍下split acti ...

  9. Python基础——原生数据类型(字典,列表,元组,字符串)

    字典Dictionary 字典定义了键值对的1对1管理. 1.定义字典 请看下面的栗子,我们先创建了一个具有两个元素的字典,每对都是key:value的形式. 我们能通过key得到value,但是不能 ...

  10. 用jQuery的attr()设置option默认选中无效的解决 attr设置属性失效

    表单下拉选项使用selected设置,发现第一次默认选中成功,在页面不刷新的情况下,再次下拉,selected属性设置了,默认选中不生效 在手机端有些浏览器用jQuery的attr()方法设置sele ...