前言

面试造火箭,工作增删改。

从上述优美的诗句可知,“增删改查”是工作的基本技能。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

准备表

  • 表结构如下
    Mysql -- 增删改查 Mysql 第1张

    查 -- select

    查询语句在此分为简单查询、条件查询、复杂查询。

    简单查询

    -- 查询全部字段 使用 * 号
    select * from customers;
    -- 查询某个字段, 输入字段名,如查询 customerName
    select  customerName from customers;
    -- 使用别名,比如将 customerName 起别名为 cname
    select  customerName cname from customers;
    select customerName as cname from customers;
    -- 多字段查询,查询 customerName 和 phone
    select customerName cname ,phone as phoneNum from customers;

    条件查询

    语法:
    SELECT 列名称 FROM 表名称 WHERE 列 运算符 值

  • 写在前面
    1. SQL 使用单引号来环绕文本值(大部分数据库系统也接受双引号)。如果是数值,请不要使用引号。
    2. SQL 语句不区分大小写

    下面的运算符可在 WHERE 子句中使用:
    Mysql -- 增删改查 Mysql 第2张
    具体操作如下:
    ```
    -- 查询 creditLimit 大于等于 3000 的数据
    select * from customers where creditLimit >=3000;
    -- 查询 city 不是USA 的数据的customerName 和phone
    select customerName ,phone from customers where city != 'USA';
    select customerName ,phone from customers where city <> 'USA';
    -- 查询 creditLimit 大于等于3000 小于等于 60000 的数据
    select * from customers where creditLimit>=3000 and creditLimit <=60000;
    -- between on 是闭区间 效果和 xx>=3000 and xxx <=60000 一样
    select * from customers where creditLimit between 3000 and 60000;
    -- or 与 in ,查询customerNumber 为 103 ,128,144 的数据
    select * from customers where customerNumber in(103 ,128,144);
    select * from customers where customerNumber =103 or customerNumber =128 or customerNumber=144;
    -- and 和or 联用
    select * from customers where (contactFirstName='Jean' or contactFirstName ='Julie') and contactLastName ='King'
    -- not in ,查询customerNumber 不包含 103 ,128,144 的数据
    select * from customers where customerNumber not in(103 ,128,144);
    -- 查询 state 为 null 的数据
    select * from customers where state is null;
    -- 排序
    select * from customers order by customerNumber;
    -- 多字段排序
    select * from customers order by customerNumber asc, creditLimit desc;
    -- 模糊查询 LIKE
    -- 查询 contactLastName 包含 h 的 数据
    select * from customers where contactLastName like "%h%";
    -- 查询 contactLastName 第一个字母是 h 的 数据
    select * from customers where contactLastName like "h%";
    -- 查询 contactLastName 最后一个字母是 h 的 数据
    select * from customers where contactLastName like "%h";
    -- 查询 contactLastName 第三个字母是 h 的数据 (_ _ h) 使用
    select * from customers where contactLastName like "__h%";
    -- 解释: 下划线_ 代表一个占位符; 百分号% 代表多个字符

    ```

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄