• MySQL之CRUD


    目录

    表结构:

    ​编辑 表数据:​编辑

    一、基础的增删改查

    查看表结构

    增 

    查询

    ​编辑修改

     删

    二、过滤查询

    过滤查询

    一、按条件表达式筛选

     二、按逻辑表达式筛选

     三、模糊查询

    is null VS <=>

    四、 order by 子句

    按单个字段排序

    添加筛选条件再排序

    按表达式排序

     按别名排序

     按函数排序

    按多个字段排序

    排序练习


    表结构:


     表数据:

    一、基础的增删改查

    查看表结构

    desc 表名

    desc t_mysql_employees;

    增 

     INSERT into 表名(列名) values(要插入列的数据);

    INSERT into t_mysql_employees(first_name,last_name,email,phone_number,salary,commission_pct,manager_id,hiredate) values('oy','x','1828190940@qq.com','155********',3000,0.25,100,now());

    我们现在看下总共有多少行

     可以看到,比之前要多一行了。

    试一下查询它,看能不能查到。

    查询

    select *from 表名 /  select *from 表名 where  查询条件

    1. select *from t_mysql_employees where phone_number='155********'
    2. select *from t_mysql_employees;

    修改

    update 表名 set 要修改数据的列名=修改后的数据  where 条件

    update t_mysql_employees set last_name='xiang' where phone_number='155********'

    再查询一下是否修改成功

     删

    delete from 表名 where 条件语句

    delete from t_mysql_employees where phone_number='155********'

    再查询一下

     查询不到,已经删除掉了。

    二、过滤查询

    过滤查询

    一、按条件表达式筛选

    案例1:查询工资>12000的员工信息

    select * from t_mysql_employees where salary>12000;

    案例2:查询部门编号不等于90号的员工名和部门编号

    select last_name,department_id from t_mysql_employees where not(department_id=90);

     二、按逻辑表达式筛选

    案例1:查询工资z在10000到20000之间的员工名、工资以及奖金

    select last_name,salary,commission_pct from t_mysql_employees where salary between 10000 and 20000;

    案例2:查询部门编号不是在90到110之间,或者工资高于15000的员工信息

    select * from t_mysql_employees where not(department_id between 90 and 110) or salary>15000;

     三、模糊查询

    like

    案例1:查询员工名中包含字符a的员工信息

    select * from t_mysql_employees where last_name like '%a%'

     案例2:查询员工名中第三个字符为e,第五个字符为a的员工名和工资

    select * from t_mysql_employees where last_name like '%e%a%';

    案例3:查询员工名中第二个字符为_的员工名 

    between and 

    查询员工编号在100到120之间的员工信息

    	select * from t_mysql_employees where employee_id between 100 and 120;

     in

    查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号

    select last_name,job_id from t_mysql_employees where job_id in ('IT_PROG','AD_VP','AD_PRES');

     is null

    查询没有奖金的员工名和奖金率

    	select last_name,commission_pct from t_mysql_employees where commission_pct is not null;

     安全等于 <=>

     查询工资为12000的员工信息

    	SELECT * FROM t_mysql_employees where salary <=> 12000;
    

     

    is null VS <=>

    IS NULL:仅仅可以判断NULL值,可读性较高,建议使用
    <=>   :既可以判断NULL值,又可以判断普通的数值,可读性较低

    四、 order by 子句

    按单个字段排序

    select * from t_mysql_employees ORDER BY salary;

    添加筛选条件再排序

    查询部门编号>=90的员工信息,并按员工编号降序

    	select * from t_mysql_employees where department_id>=90 ORDER BY employee_id desc;
    

    按表达式排序

    查询员工信息 按年薪降序

     按别名排序

    查询员工信息 按年薪升序

    SELECT em.salary*12*(1+IFNULL(commission_pct,0)),em.* FROM t_mysql_employees em ORDER BY em.salary*12*(1+IFNULL(commission_pct,0))

     按函数排序

    查询员工名,并且按名字的长度降序

    SELECT last_name,COUNT(first_name,last_name),LENGTH(CONCAT(first_name,last_name)) from t_mysql_employees ORDER BY LENGTH(CONCAT(first_name,last_name)) desc;

    按多个字段排序

    查询员工信息,要求先按工资降序,再按employee_id升序

    	select * from t_mysql_employees ORDER BY salary desc,employee_id asc;

    排序练习

    查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序

    	SELECT first_name,last_name,department_id,salary*12*(1+IFNULL(commission_pct,0)) from t_mysql_employees ORDER BY salary*12*(1+IFNULL(commission_pct,0)) desc,first_name asc,last_name asc;

     选择工资不在8000到17000的员工的姓名和工资,按工资降序

    select first_name,last_name,salary from t_mysql_employees where not(salary between 8000 and 17000) ORDER BY salary desc;

     查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序

    select * from t_mysql_employees em where email like '%e%' ORDER BY department_id asc,LENGTH(email) desc;


      OK,今日的学习就到此结束啦,如果对个位看官有帮助的话可以留下免费的赞哦(收藏或关注也行),如果文章中有什么问题或不足以及需要改正的地方可以私信博主,博主会做出改正的。个位看官,小陽在此跟大家说拜拜啦!

  • 相关阅读:
    Springboot毕业设计毕设作品,校园疫情防控小程序系统 开题报告
    玩转Mysql系列 - 第21篇:什么是索引?
    Unreal Property System (Reflection) 虚幻属性系统(反射)
    uni-app:实现request请求的递归(设置request请求的访问次数),并且调用自定义方法给出返回值
    Elasticsearch第二篇:es之间版本比较
    关于 Vite 的浅显学习 - 功能 - TypeScript
    【SEC 学习】注册表添加启动脚本
    (三)使用 Vue 脚手架
    PAT 1056 Mice and Rice(模拟比赛,同一轮比赛 多个场次)
    Subset Sum 问题单个物品重量限制前提下的更优算法
  • 原文地址:https://blog.csdn.net/weixin_65211978/article/details/125592837