把博客园图标替换成自己的图标
把博客园图标替换成自己的图标end
旋转立方体
旋转立方体end

掌握 MySQL3

十二、修改表的完整语句

  1、修改表名

    alter table 表名

      rename 新表名  

  2、增加字段

    alter table 表名

      add 字段名 数据类型 [完整性约束条件...]

      add 字段名 数据类型 [完整性约束条件...]

    alter table 表名

      add 字段名 数据类型 [完整性约束条件...] first

    alter table 表名

      add 字段名 数据类型 [完整性约束条件...] after 字段名;

  3、删除字段

    after table 表名

      drop 字段名

  4、修改字段  # modify 只能改字段数据类型和完整约束,不能改字段名,change 可以。

    alter table 表名

      modify 字段名 数据类型 [完整性约束条件...]

    alter table 表名

      change 旧字段名 新字段名 旧数据类型 [完整性约束条件...]

    alter table 表名

      change 旧字段名 新字段名 新数据类型 [完整性约束条件...]

 

十三、基本的查询语句及方法

  13.1、where  

    1、查询 id 大于等于 3 小于等于 6 的数据

      select * from emp where id >=3 and id <= 6;

      select * from emp between 3 ande 6:

    2、查询薪资是20000或18000或17000的数据

      select * from emp where salary = 2000 or salary = 18000 or salary = 17000;

      select * from emp where salary in (20000,18000,17000);

    3、模糊匹配  like

      %:匹配多个任意字符

      _:匹配一个任意字符

    查询员工姓名中包含 o 字母的员工姓名和工资

      select name,salary from emp where name like '%o%'; 

    查询员工姓名是由四个字符组成的员工姓名与其薪资

      select name,salary from emp where name like '____';

    4、查询岗位描述为空的员工名与岗位名

      select name,post from emp where post_comment is null;

  13.2、group by  分组

    分组后最小单位是组,而不再展示组内单个数据的信息,但可以通过其他方法(聚合函数)获取。

    1、按部门分组

      select * from group by post;

      如果 MySQL 不报错,说明没有设置严格模式

      

      show variables like '%mode%';

      set session  当前窗口有效

      set globle    全局有效

      set globle sql_mode='strict_trans_tables,only_full_group_by';

      

        select post from emp group by post;

    2、聚合函数(max,min avg,sum,count)

 

      1、获取每个部门的最高工资(max)        

        select post,max(salary) from emp group by post;

        给字段取别名

        select post as '部门',max(salary) as '最高工资' from emp group by post;

      2、获取每个部门的人数(count)

        在统计分组内个数的时候,填写任意非空字段都可以完成计数

        select post,count(age) from emp group by post;

      3、查询分组后的部门名称和每个部门下所有的学生姓名(concat)

        concat 可以用来拼接数据

          concat  不分组的情况下使用

          group_concat  分组之后使用

        select concat('NAME:',name),concat('SAL:',salary) from emp;

        select post,group_concat(name,':',salary) from emp group by post;

      4、查询员工年薪

        select name,salary*12 from emp;

  13.3、having

    效果和 where 一样,也是用来筛选数据,但 having 跟在group by 之后

    where 是对整体数据做一个初步筛选

    having 是对分组之后的数据再进行一次针对性的筛选

    统计个部门年龄在 30 岁以上的员工平均工资,并保留平局工资大于 10000 的部门

      select post,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000;

   13.4、distinct  去重(针对重复的数据)

    select distinct id,age from emp;

  13.5、order by  排序

    默认是升序  asc

    可以改成降序  desc

    select * from emp order by salary;

    select * from emp order by salary asc;

    select * from  emp order by salary desc;

    select * from emp order by age,salary;  先按照 age 做升序,相同的情况下按照 salary 做升序

    select * from emp order by age asc,salary desc;

  13.6、限制展示数据的条数

    select * from emp limit 5;  # 展示5条数据

    select * from emp limit 3,5;  # 由 4 起始展示 5 条数据  

    查询工资最高的人的详细信息

      先按照薪资排序

      使用 limit 限制,只取一条

    select * from emp order by salary desc limit 1;

  13.7、正则

    select * from emp where name regexp '^j.*(n|y)$;

  13.8、多表查询

    表查询分为两大类

      1、连表查询

      2、子查询

    select * from emp,dep;  # 笛卡尔积

    查询员工与部门信息  

      select * from emp,dep where emp.dep_id = dep.id;

    查询部门为技术部的员工和部门信息

      select * from emp,dep where emp.dep_id = dep.id and dep.name = ‘技术’

    

    有专门帮你做连表的方法

    内连接(inner join):只取两张表有对应关系的记录

      select * from emp inner join dep on emp.dep_id = dep.id;

    左连接(left join):在内连接的基础上保留左表没有对应关系的记录,左表空余记录用 null 填充

      select * from emp left join dep on emp.dep_id = dep.id;

    右连接(right join):在内连接基础上保留右表没有对应关系的记录

      select * from emp right join dep on emp.dep_id = dep.id;

    全连接(union):在内连接基础上保留左、右表没有对应关系的记录

      select * from emp right join dep on emp.dep_id = dep.id;

      union

      selecr * from emp left join dep on emp.dep_id = dep.id;

  13.9、将一张表的查询结果作为另外一个 sql 语句的查询条件

    select name from dep where id = (select dep_id from emp where name = 'a');

 

 

    

    

posted @ 2019-08-22 20:52  远翔、  阅读(183)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end