经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库运维 » MySQL » 查看文章
MySQL数据库之单双表查询
来源:cnblogs  作者:爱吃玉米的小男孩  时间:2018/9/25 19:48:44  对本文有异议

单表查询

先创建表

  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. mysql> desc employee;
  16. +--------------+-----------------------+------+-----+---------+----------------+
  17. | Field | Type | Null | Key | Default | Extra |
  18. +--------------+-----------------------+------+-----+---------+----------------+
  19. | id | int(11) | NO | PRI | NULL | auto_increment |
  20. | name | varchar(20) | NO | | NULL | |
  21. | sex | enum('male','female') | NO | | male | |
  22. | age | int(3) unsigned | NO | | 28 | |
  23. | hire_date | date | NO | | NULL | |
  24. | post | varchar(50) | YES | | NULL | |
  25. | post_comment | varchar(100) | YES | | NULL | |
  26. | salary | double(15,2) | YES | | NULL | |
  27. | office | int(11) | YES | | NULL | |
  28. | depart_id | int(11) | YES | | NULL | |
  29. +--------------+-----------------------+------+-----+---------+----------------+
  30.  
  31. #插入记录
  32. #三个部门:教学,销售,运营
  33. insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
  34. ('egon','male',18,'20170301','teacher',7300.33,401,1), #以下是教学部
  35. ('alex','male',78,'20150302','teacher',1000000.31,401,1),
  36. ('wupeiqi','male',81,'20130305','teacher',8300,401,1),
  37. ('yuanhao','male',73,'20140701','teacher',3500,401,1),
  38. ('liwenzhou','male',28,'20121101','teacher',2100,401,1),
  39. ('jingliyang','female',18,'20110211','teacher',9000,401,1),
  40. ('jinxin','male',18,'19000301','teacher',30000,401,1),
  41. ('成龙','male',48,'20101111','teacher',10000,401,1),
  42. ('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
  43. ('丫丫','female',38,'20101101','sale',2000.35,402,2),
  44. ('丁丁','female',18,'20110312','sale',1000.37,402,2),
  45. ('星星','female',18,'20160513','sale',3000.29,402,2),
  46. ('格格','female',28,'20170127','sale',4000.33,402,2),
  47. ('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
  48. ('程咬金','male',18,'19970312','operation',20000,403,3),
  49. ('程咬银','female',18,'20130311','operation',19000,403,3),
  50. ('程咬铜','male',18,'20150411','operation',18000,403,3),
  51. ('程咬铁','female',18,'20140512','operation',17000,403,3)
  52. ;
  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. SELECT * FROM employee;
  5. SELECT name,salary FROM employee;
  6. #避免重复DISTINCT
  7. SELECT DISTINCT post FROM employee;
  8. #通过四则运算查询
  9. SELECT name, salary*12 FROM employee;
  10. SELECT name, salary*12 AS Annual_salary FROM employee;
  11. SELECT name, salary*12 Annual_salary FROM employee;
  12. #定义显示格式
  13. CONCAT() 函数用于连接字符串
  14. SELECT CONCAT('姓名: ',name,' 年薪: ', salary*12) AS Annual_salary
  15. FROM employee;
  16. CONCAT_WS() 第一个参数为分隔符
  17. SELECT CONCAT_WS(':',name,salary*12) AS Annual_salary
  18. FROM employee;

小练习:

  1. 1 查出所有员工的名字,薪资,格式为
  2. <名字:egon> <薪资:3000>
  3. select concat('<名字:',name,'> ' ,'<薪资:',salary,'>' ) from employee;
  4. 2 查出所有的岗位(去掉重复)
  5. select distinct depart_id from employee;
  6. 3 查出所有员工名字,以及他们的年薪,年薪的字段名为年薪
  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. #2:多条件查询
  5. SELECT name,salary FROM employee
  6. WHERE post='teacher' AND salary>10000;
  7. #3:关键字BETWEEN AND
  8. SELECT name,salary FROM employee
  9. WHERE salary BETWEEN 10000 AND 20000;
  10. SELECT name,salary FROM employee
  11. WHERE salary NOT BETWEEN 10000 AND 20000;
  12. #4:关键字IS NULL(判断某个字段是否为NULL不能用等号,需要用IS)
  13. SELECT name,post_comment FROM employee
  14. WHERE post_comment IS NULL;
  15. SELECT name,post_comment FROM employee
  16. WHERE post_comment IS NOT NULL;
  17. SELECT name,post_comment FROM employee
  18. WHERE post_comment=''; 注意''是空字符串,不是null
  19. ps
  20. 执行
  21. update employee set post_comment='' where id=2;
  22. 再用上条查看,就会有结果了
  23. #5:关键字IN集合查询
  24. SELECT name,salary FROM employee
  25. WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ;
  26. SELECT name,salary FROM employee
  27. WHERE salary IN (3000,3500,4000,9000) ;
  28. SELECT name,salary FROM employee
  29. WHERE salary NOT IN (3000,3500,4000,9000) ;
  30. #6:关键字LIKE模糊查询
  31. 通配符’%
  32. SELECT * FROM employee
  33. WHERE name LIKE 'eg%';
  34. 通配符’_
  35. SELECT * FROM employee
  36. WHERE name LIKE 'al__';

四.having过滤

having和where语法上是一样的。

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

不同点:

  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. 查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
  2. select post,group_concat(name) 员工姓名,count(id) 个数 from employee group by post having count(id)<2;
  3. 2. 查询各岗位平均薪资大于10000的岗位名、平均工资
  4. select post,avg(salary) from employee group by post having avg(salary)>10000;
  5. 3. 查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资
  6. select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000;

五、分组查询 group by

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

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

强调:

  1. 分组:一般相同的多的话就可以分成一组(一定是有重复的字段)
    小练习:
  1. 1. 查询岗位名以及岗位包含的所有员工名字
  2. select post,group_concat(name) from employee group by post;
  3. 2. 查询岗位名以及各岗位内包含的员工个数
  4. select post,count(id) from employee group by post;
  5. 3. 查询公司内男员工和女员工的个数
  6. select sex,count(id) from employee group by sex;
  7. 4. 查询岗位名以及各岗位的平均薪资
  8. select post,max(salary) from employee group by post;
  9. 5. 查询岗位名以及各岗位的最高薪资
  10. select post,max(salary) from employee group by post;
  11. 6. 查询岗位名以及各岗位的最低薪资
  12. select post,min(salary) from employee group by post;
  13. 7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
  14. select sex,avg(salary) from employee group by sex;

六、关键字的执行优先级(重点)

  1. 重点中的重点:关键字的执行优先级
  2. from
  3. where
  4. group by
  5. having
  6. select
  7. distinct
  8. order by
  9. limit

1.找到表:from

2.拿着where指定的约束条件,去文件/表中取出一条条记录

3.将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组

4.如果有聚合函数,则将组进行聚合

5.将4的结果过滤:having

6.查出结果:select

7.去重

8.将6的结果按条件排序:order by

9.将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. 按多列排序:先按照age排序,如果年纪相同,则按照薪资排序
  6. SELECT * from employee
  7. ORDER BY age,
  8. salary DESC;
  9. ===========order by==========
  10. 1.select * from employee order by salary;#如果不指定,默认就是升序
  11. 2.select * from employee order by salary asc;
  12. 3.select * from employee order by salary desc;
  13. #先按照年龄升序,当年龄相同的太多,分不清大小时,在按照工资降序
  14. 4.select * from employee order by age asc, salary desc;

多表查询

一.介绍

首先先准备表

员工表和部门表

  1. #建表
  2. create table department(
  3. id int,
  4. name varchar(20)
  5. );
  6. create table employee1(
  7. id int primary key auto_increment,
  8. name varchar(20),
  9. sex enum('male','female') not null default 'male',
  10. age int,
  11. dep_id int
  12. );
  13. #插入数据
  14. insert into department values
  15. (200,'技术'),
  16. (201,'人力资源'),
  17. (202,'销售'),
  18. (203,'运营');
  19. insert into employee1(name,sex,age,dep_id) values
  20. ('egon','male',18,200),
  21. ('alex','female',48,201),
  22. ('wupeiqi','male',38,201),
  23. ('yuanhao','female',28,202),
  24. ('liwenzhou','male',18,200),
  25. ('jingliyang','female',18,204)
  26. ;

查看

二、多表连接查询

1.交叉连接:不适用任何匹配条件。生成笛卡尔积、

  1. select * from employee1 ,department;

2.内连接:找两张表共有的部分,相当于利用条件从笛卡尔积结果中筛选出了正确的结果。(只连接匹配的行)

  1. #找两张表共有的部分,相当于利用条件从笛卡尔积结果中筛选出了正确的结果
  2. #department没有204这个部门,因而employee表中关于204这条员工信息没有匹配出来
  3. select * from employee1,department where employee1.dep_id=department.id;
  4. #上面用where表示的可以用下面的内连接表示,建议使用下面的那种方法
  5. select * from employee1 inner join department on employee1.dep_id=department.id;
  6. #也可以这样表示哈
  7. select employee1.id,employee1.name,employee1.age,employee1.sex,department.name from
  8. employee1,department where employee1.dep_id=department.id;

注意:内连接的join可以忽略不写,但是还是加上看起来清楚点

3.左连接:优先显示左表全部记录。

  1. #左链接:在按照on的条件取到两张表共同部分的基础上,保留左表的记录
  2. select * from employee1 left join department on department.id=employee1.dep_id;
  3. select * from department left join employee1 on department.id=employee1.dep_id;

4.右链接:优先显示右表全部记录。

  1. #右链接:在按照on的条件取到两张表共同部分的基础上,保留右表的记录
  2. select * from employee1 right join department on department.id=employee1.dep_id;
  3. select * from department right join employee1 on department.id=employee1.dep_id;

5.全外连接:显示左右两个表的全部记录。

  1. 注意:mysql不支持全外连接 full join
  2. 强调:mysql可以使用union间接实现全外连接
  1. select * from employee1 left join department on department.id=employee1.dep_id
  2. union
  3. select * from employee1 right join department on department.id=employee1.dep_id;

三、符合条件连接查询

  1. 示例1:以内连接的方式查询employeedepartment表,并且employee表中的age字段值必须大于25,
    即找出公司所有部门中年龄大于25岁的员工
  1. select * from employee1 inner join department on employee1.dep_id=department.id
  2. and age>25;
  1. 示例2:以内连接的方式查询employeedepartment表,并且以age字段的升序方式显示
  1. select * from employee1 inner join department on employee1.dep_id=department.id
  2. =
  3.  
  4. and age>25 and age>25 order by age asc;

四、子查询

  1. #1:子查询是将一个查询语句嵌套在另一个查询语句中。
  2. #2:内层查询语句的查询结果,可以为外层查询语句提供查询条件。
  3. #3:子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
  4. #4:还可以包含比较运算符:= 、 !=、> 、<等

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号