学习MySQL基础笔记4

旧城等待, 2024-03-26 15:43 152阅读 0赞

下一次笔记就是MySQL的进阶了

  1. #作业
  2. create table employee(
  3. id int unsigned primary key auto_increment comment '编号',
  4. job_id int unsigned not null comment '工种编号',
  5. name varchar(30) not null comment '名称',
  6. department_id int(3) zerofill not null comment '部门编号',
  7. salary decimal(10,2) not null default 0 comment '薪水',
  8. bonus decimal(10,2) not null default 0 comment '奖金'
  9. )
  10. insert into employee(job_id,name,department_id,salary,bonus) values
  11. (1,'tom',1,6000,1000),
  12. (1,'jerry',1,7000,1000),
  13. (2,'alice',1,6700,1000),
  14. (3,'tina',1,8000,1000),
  15. (1,'zhangs',2,9000,1000),
  16. (2,'lisi',2,9000,0),
  17. (3,'zhaowu',2,9000,0);
  18. #(1) 使用alter, 添加 se(性别 tinyint 值 0,1 ) age(年龄 int ) 字段并且放在 name 后面
  19. alter table employee
  20. add se tinyint unsigned not null after name,
  21. add age int unsigned not null after name;
  22. #(2) 使用alter,修改 se 字段名为 sex
  23. alter table employee change se sex tinyint unsigned not null;
  24. #(3) 条件查询
  25. #a.查找 employee 中 年龄大于等于 20并且 薪水大于5000的记录
  26. select * from employee where age>=20 and salary>5000;
  27. #b. 查找 employee 中 编号在2-9 也可以 sex 是 1的记录
  28. select * from employee where id between 2 and 9 or sex=1;
  29. #c. 查找 employee 中 编号是偶数并且年龄大于22的记录
  30. select * from employee where id%2=0 and age>22;
  31. #d. 条件编号在2-9 的记录,更新age 字段的年龄都加9
  32. update employee set age=age+9 where id between 2 and 9 ;
  33. #(4) 分组
  34. #a. 查询每个部门,显示 部门编号, 人数 ,最大薪水,最小薪水,薪水和
  35. select department_id,
  36. count(*) as 人数,
  37. max(salary) as msalary,
  38. sum(salary),
  39. min(salary)
  40. from employee group by department_id;
  41. #b. 查询每个工种,显示 工种编号, 人数 ,最大年龄,最小年龄,平均年龄
  42. select job_id,
  43. count(*) as 人数,
  44. max(age) as mage,
  45. avg(age),
  46. min(age)
  47. from employee group by job_id;
  48. # [having 条件] 二次过滤: 结合group by 使用
  49. # eg1 查询部门人数大于2 的部门编号和人数的记录
  50. select department_id,count(*) as c
  51. from employee
  52. group by department_id
  53. having c >2;
  54. select department_id,count(*) as c
  55. from employee
  56. where id between 2 and 6
  57. group by department_id
  58. having c >2;
  59. # [order by 字段] 排序
  60. # 默认升序 asc,降序 desc
  61. # eg2 查询 employee 表 2-6的记录显示 编号,用户名称,薪水并且对薪水和编号降序排序
  62. select id,name,salary from employee
  63. where id between 2 and 6
  64. order by salary desc,id desc;
  65. # [limit 偏移量,长度] 截取 -- slice
  66. # a.偏移量: 起始索引,索引从0开始
  67. # b. 长度: 显示条数(记录数)
  68. # c. 分页 偏移量 = (当前页 -1 )*长度
  69. # eg3 查询前三条记录
  70. select * from employee where id <=3; # 不准确
  71. select * from employee limit 0,3;
  72. # eg4 分页 (新闻内容)
  73. # 第一页 每页显示3条,降序排序 偏移量 = (当前页 -1 )*长度= (1-1)*3=0
  74. select * from employee order by id desc limit 0,3;
  75. # 第二页 每页显示3条,降序排序 偏移量 = (当前页 -1 )*长度= (2-1)*3=3
  76. select * from employee order by id desc limit 3,3;
  77. # 第三页 每页显示3条,降序排序 偏移量 = (当前页 -1 )*长度= (3-1)*3=6
  78. select * from employee order by id desc limit 6,3;
  79. # 多表联合查询
  80. # job 表
  81. create table job(
  82. id int unsigned primary key auto_increment comment '编号',
  83. jname varchar(30) not null comment '工种名称'
  84. );
  85. insert into job(jname) values
  86. ('电工'),
  87. ('汽修'),
  88. ('瓦工'),
  89. ('水泥工');
  90. # depart 表
  91. create table depart(
  92. id int(3) zerofill primary key auto_increment comment '编号',
  93. dname varchar(30) not null comment '部门名称'
  94. );
  95. insert into depart(dname) values
  96. ('装修'),
  97. ('销售'),
  98. ('会计');
  99. /*
  100. SELECT 表.字段名称,表.字段名称...
  101. FROM 表1
  102. 连接类型 表2 ON 条件
  103. 连接类型 表3 ON 条件...
  104. 连接类型:
  105. (1)内连接: [INNER] JOIN 查找两个表都符合条件的信息
  106. */
  107. # eg5 查询employee 表中 员工编号,姓名(employee),部门名(depart)的记录
  108. select e.id,e.name,d.dname
  109. from employee as e
  110. inner join depart as d on e.department_id = d.id;
  111. # 了解(sql92)
  112. select e.id,e.name,d.dname
  113. from employee as e,depart as d where e.department_id = d.id;
  114. # eg6 查询employee 表中 员工编号,姓名(employee),部门名(depart),工种名称(job)的记录
  115. select e.id,e.name,d.dname,j.jname
  116. from employee as e
  117. inner join depart as d on e.department_id = d.id
  118. inner join job as j on e.job_id = j.id;
  119. # eg7 查询部门人数大于2的部门名(depart)和人数的记录并且对人数降序排序
  120. select d.dname,count(*) as c
  121. from employee as e
  122. inner join depart as d on e.department_id = d.id
  123. group by e.department_id
  124. having c > 2
  125. order by c desc;
  126. # eg8 查询部门名(depart)是'装修'和'会计'的员工信息(employee)的记录
  127. select e.*,d.dname
  128. from employee as e
  129. inner join depart as d on e.department_id = d.id
  130. where d.dname in ('装修','会计');
  131. /*
  132. (2)外连接:
  133. 外连接的查询结果为主表中的所有记录如果从表中有和它匹配的,则显示匹配的值如果从表中没有和它匹配的,则显示null
  134. 外连接查询结果=内连接结果+主表中有而从表没有的记录.左外连接,left join左边的是主表,右外连接,right join右边的是主表
  135. 全外连接=内连接的结果+表1中有但表2没有的+表2中有但表1没有的 full join
  136. */
  137. # eg9 查询所有部门的部门名称及对应员工信息的记录
  138. # left join 左外连接 (左边是主表)
  139. select d.dname,e.*
  140. from depart as d
  141. left join employee as e on d.id = e.department_id;
  142. # right join 右外连接(右边是主表)
  143. select d.dname,e.*
  144. from employee as e
  145. right join depart as d on d.id = e.department_id;
  146. # 内连接-自连接(菜单级联)
  147. #递归用法
  148. # 设计 一个菜单表 menu (子菜单pid 与上一级菜单id 相等)
  149. # id name pid
  150. # 1 家电 0
  151. # 2 手机 0
  152. # 3 电脑 0
  153. # 4 电视 1
  154. # 5 空调 1
  155. # 6 洗衣机 1
  156. # 7 长虹 4
  157. # 8 TCL 4
  158. # 9 格力 5
  159. # 10 美的 5
  160. create table menu(
  161. id int unsigned primary key auto_increment comment '编号',
  162. name varchar(30) not null comment '菜单名称',
  163. pid int unsigned not null comment '上一级菜单id'
  164. );
  165. insert into menu(name,pid) values
  166. ('家电',0),
  167. ('手机',0),
  168. ('电脑',0),
  169. ('电视',1),
  170. ('空调',1),
  171. ('洗衣机',1),
  172. ('长虹',4),
  173. ('TCL',4),
  174. ('格力',5),
  175. ('美的',5);
  176. # eg10: 查询子菜单名称及对应父菜单的名称的记录
  177. select s.name as 子菜单 ,p.name as 父菜单
  178. from menu as p
  179. inner join menu as s on s.pid = p.id;

发表评论

表情:
评论列表 (有 0 条评论,152人围观)

还没有评论,来说两句吧...

相关阅读