【MySQL】表的增删查改(CRUD)(下)

06-01 1178阅读

【MySQL】表的增删查改(CRUD)(下)

个人主页:♡喜欢做梦

欢迎  👍点赞  ➕关注  ❤️收藏  💬评论


本篇是继上篇的下篇,如果上篇没有看过小伙伴,可以先看看我的上一篇再来看一下这一篇【MySQL】表的增删查改(CRUD)(上) 

💥条件查询(where)

概念: 

条件查询:允许用户在查询语句中指定筛选条件,数据库中会根据这些条件从表中筛选出符合条件的数据,将满足条件的记录返回给用户,不满足的条件的排出在外。

  • 进行条件查询需要用到where,否则在没有写where的情况下,写运算符会发生报错。

    运算符 

    比较运算符:

    运算符说明
    >,>=,60; +------+--------+---------+------+---------+ | id | name | chinese | math | english | +------+--------+---------+------+---------+ | 1 | 张三 | 67.0 | 80.0 | 88.0 | | 2 | 李四 | 87.0 | 55.0 | 66.0 | | 3 | 小五 | 91.0 | 90.0 | 96.0 | | 5 | 赵六 | 91.0 | 90.0 | 96.0 | | 8 | 孙武 | 77.0 | 88.0 | NULL | +------+--------+---------+------+---------+ 5 rows in set (0.00 sec) --查询英语成绩小于60的同学(其结果集会自动过滤null) mysql> select * from exam where english select * from exam where math=90; +------+--------+---------+------+---------+ | id | name | chinese | math | english | +------+--------+---------+------+---------+ | 3 | 小五 | 91.0 | 90.0 | 96.0 | | 5 | 赵六 | 91.0 | 90.0 | 96.0 | +------+--------+---------+------+---------+ 2 rows in set (0.00 sec) --查询数学成绩为85的同学(=) mysql> select * from exam where math=85; --不存在数学成绩为85的同学,显示为空 Empty set (0.00 sec) --查询数学成绩不为90的同学(或者!=) mysql> select * from exam where math90; +------+--------+---------+------+---------+ | id | name | chinese | math | english | +------+--------+---------+------+---------+ | 1 | 张三 | 67.0 | 80.0 | 88.0 | | 2 | 李四 | 87.0 | 55.0 | 66.0 | | 4 | 小六 | 48.0 | 32.0 | 55.0 | | 6 | 老王 | 23.0 | 36.0 | NULL | | 7 | 老六 | 33.0 | 26.0 | 66.0 | | 8 | 孙武 | 77.0 | 88.0 | NULL | +------+--------+---------+------+---------+ 6 rows in set (0.00 sec) --查询语数英总分小于150的同学 mysql> select *from exam where chinese+english+math select name,chinese+math+english as total from exam where total select name,chinese+math+english as total from exam where chinese+english+math select * from exam where math40 and englishand>or;
  • 建议:用的时候还是手动加括号; 

  • 范围查询

    1.between...and...

    --查询英语成绩在60-90的同学(between a and b)
    mysql>  select * from exam where english between 60 and 90;
    +------+--------+---------+------+---------+
    | id   | name   | chinese | math | english |
    +------+--------+---------+------+---------+
    |    1 | 张三   |    67.0 | 80.0 |    88.0 |
    |    2 | 李四   |    87.0 | 55.0 |    66.0 |
    |    7 | 老六   |    33.0 | 26.0 |    66.0 |
    +------+--------+---------+------+---------+
    3 rows in set (0.00 sec)
    --也可以使用and
    mysql> select * from exam where english >=60 and english  select * from exam where chinese in(33,87,66);
    +------+--------+---------+------+---------+
    | id   | name   | chinese | math | english |
    +------+--------+---------+------+---------+
    |    2 | 李四   |    87.0 | 55.0 |    66.0 |
    |    7 | 老六   |    33.0 | 26.0 |    66.0 |
    +------+--------+---------+------+---------+
    2 rows in set (0.01 sec)
    --也可以使用or来表示
    mysql> select * from exam where chinese=33 or chinese=87 or  chinese=66;
    +------+--------+---------+------+---------+
    | id   | name   | chinese | math | english |
    +------+--------+---------+------+---------+
    |    2 | 李四   |    87.0 | 55.0 |    66.0 |
    |    7 | 老六   |    33.0 | 26.0 |    66.0 |
    +------+--------+---------+------+---------+
    2 rows in set (0.01 sec)

     🔥模糊查询:like
    --查找老开头的名字(%):
    mysql> select * from exam where name like '老%';
    +------+-----------+---------+------+---------+
    | id   | name      | chinese | math | english |
    +------+-----------+---------+------+---------+
    |    6 | 老王      |    23.0 | 36.0 |    NULL |
    |    7 | 老六      |    33.0 | 26.0 |    66.0 |
    |    8 | 老小四    |    22.0 | 87.0 |    92.0 |
    +------+-----------+---------+------+---------+
    3 rows in set (0.00 sec)
    --查找最后一个名字为六的名字:
    mysql> select * from exam where name like '%六';
    +------+--------+---------+------+---------+
    | id   | name   | chinese | math | english |
    +------+--------+---------+------+---------+
    |    4 | 小六   |    48.0 | 32.0 |    55.0 |
    |    5 | 赵六   |    91.0 | 90.0 |    96.0 |
    |    7 | 老六   |    33.0 | 26.0 |    66.0 |
    +------+--------+---------+------+---------+
    3 rows in set (0.00 sec)
    --查找名字开头为老,且名只有一个的名字:
    mysql>  select * from exam where name like '老_';
    +------+--------+---------+------+---------+
    | id   | name   | chinese | math | english |
    +------+--------+---------+------+---------+
    |    6 | 老王   |    23.0 | 36.0 |    NULL |
    |    7 | 老六   |    33.0 | 26.0 |    66.0 |
    +------+--------+---------+------+---------+
    2 rows in set (0.00 sec)
    --查找语文成绩为9开头的同学:
    mysql>  select * from exam where chinese like '9%';
    +------+--------+---------+------+---------+
    | id   | name   | chinese | math | english |
    +------+--------+---------+------+---------+
    |    3 | 小五   |    91.0 | 90.0 |    96.0 |
    |    5 | 赵六   |    91.0 | 90.0 |    96.0 |
    +------+--------+---------+------+---------+
    2 rows in set (0.00 sec)
    • 查询时要使用 like 不能使用 = ;
    • %表示任意多个(包括0个)任意字符;
    • _表示任意一个字符;
    • 要记得加上单引号,否则报错;
       🔥Null查询
      --查找英语成绩为null的同学:
      mysql>  select * from exam where english is null;
      +------+--------+---------+------+---------+
      | id   | name   | chinese | math | english |
      +------+--------+---------+------+---------+
      |    6 | 老王   |    23.0 | 36.0 |    NULL |
      |    8 | 孙武   |    77.0 | 88.0 |    NULL |
      +------+--------+---------+------+---------+
      2 rows in set (0.00 sec)
      --查找英语成绩不为null的同学:
      mysql> select * from exam where english is  not null;
      +------+-----------+---------+------+---------+
      | id   | name      | chinese | math | english |
      +------+-----------+---------+------+---------+
      |    1 | 张三      |    67.0 | 80.0 |    88.0 |
      |    2 | 李四      |    87.0 | 55.0 |    66.0 |
      |    3 | 小五      |    91.0 | 90.0 |    96.0 |
      |    4 | 小六      |    48.0 | 32.0 |    55.0 |
      |    5 | 赵六      |    91.0 | 90.0 |    96.0 |
      |    7 | 老六      |    33.0 | 26.0 |    66.0 |
      |    8 | 老小四    |    22.0 | 87.0 |    92.0 |
      +------+-----------+---------+------+---------+
      7 rows in set (0.00 sec)
      --也可以使用
      mysql>  select * from exam where english  null;
      +------+--------+---------+------+---------+
      | id   | name   | chinese | math | english |
      +------+--------+---------+------+---------+
      |    6 | 老王   |    23.0 | 36.0 |    NULL |
      |    8 | 孙武   |    77.0 | 88.0 |    NULL |
      +------+--------+---------+------+---------+
      2 rows in set (0.00 sec)
      • 查询null值时,不能使用=,!=,否则显示为空;

        【MySQL】表的增删查改(CRUD)(下)

        • 也可以使用来查找为null的值; 
          🔥分页查询:limit

          概念: 

          分页查询:用于将大量数据按照一定的大小进行划分,以便逐页获取和展示数据。 

          语法:

          --从0开始筛选n条结果:
          select * from table_name [where...][order by...] limit n;
          --从s开始筛选n条结果:
          select * from table_name [where...][order by...] limit s,n;
          --从s开始筛选n条结果(更明确):
          select * from table_name [where...][order by...] limit n offset s;
          • 起始下标是由0开始;
          • s表示起始位置;
          • n表示每页显示的记录数; 
          • offset表示起始位置;

             示例:

            --从第一条开始,记录数为0:
            --写法一:
            mysql>  select * from exam limit 3;
            +------+--------+---------+------+---------+
            | id   | name   | chinese | math | english |
            +------+--------+---------+------+---------+
            |    1 | 张三   |    67.0 | 80.0 |    88.0 |
            |    2 | 李四   |    87.0 | 55.0 |    66.0 |
            |    3 | 小五   |    91.0 | 90.0 |    96.0 |
            +------+--------+---------+------+---------+
            3 rows in set (0.00 sec)
            --写法二:
            mysql> select * from exam limit 0,3;
            +------+--------+---------+------+---------+
            | id   | name   | chinese | math | english |
            +------+--------+---------+------+---------+
            |    1 | 张三   |    67.0 | 80.0 |    88.0 |
            |    2 | 李四   |    87.0 | 55.0 |    66.0 |
            |    3 | 小五   |    91.0 | 90.0 |    96.0 |
            +------+--------+---------+------+---------+
            3 rows in set (0.00 sec)
            --写法三:
            mysql>  select * from exam limit 3 offset 0;
            +------+--------+---------+------+---------+
            | id   | name   | chinese | math | english |
            +------+--------+---------+------+---------+
            |    1 | 张三   |    67.0 | 80.0 |    88.0 |
            |    2 | 李四   |    87.0 | 55.0 |    66.0 |
            |    3 | 小五   |    91.0 | 90.0 |    96.0 |
            +------+--------+---------+------+---------+
            3 rows in set (0.00 sec)
            •  每一页起始位置的计算公式:s=(当前页号-1)*每页显示的记录数;

              3.✨修改(update)

              语法:

              UPDATE table_name SET column = expr [, column = expr ...]
               [WHERE ...] [ORDER BY ...] [LIMIT ...]
              • update:表示制定要更新的表;
              • set:用于指定要更新的列和新值,也可以更新多个列,用逗号隔开;
              • where:可选句子,用于指定更新的条件。如果不使用where句子,那么将更新所有行。

                示例:

                --更新单条数据:
                --将编号为1的同学姓名更改为王五
                mysql> update exam set name = '王五' where id=1;
                Query OK, 1 row affected (0.10 sec)
                Rows matched: 1  Changed: 1  Warnings: 0
                --更新多条记录
                --将语文成绩小于60的同学成绩加上0.6:
                mysql>  update exam set chinese=chinese+0.7 where chinese  update exam set math=math+0.5 ;
                Query OK, 9 rows affected (0.10 sec)
                Rows matched: 9  Changed: 9  Warnings: 0
                mysql>  select * from exam;
                +------+-----------+---------+------+---------+
                | id   | name      | chinese | math | english |
                +------+-----------+---------+------+---------+
                |    1 | 王五      |    67.0 | 80.5 |    88.0 |
                |    2 | 李四      |    87.0 | 55.5 |    66.0 |
                |    3 | 小五      |    91.0 | 90.5 |    96.0 |
                |    4 | 小六      |    48.7 | 32.5 |    55.0 |
                |    5 | 赵六      |    91.0 | 90.5 |    96.0 |
                |    6 | 老王      |    23.7 | 36.5 |    NULL |
                |    7 | 老六      |    33.7 | 26.5 |    66.0 |
                |    8 | 孙武      |    77.0 | 88.5 |    NULL |
                |    8 | 老小四    |    22.7 | 87.5 |    92.0 |
                +------+-----------+---------+------+---------+
                9 rows in set (0.00 sec)

                4.✨删除(delete)

                语法:

                delete from table_name [where...][order by...][limit...];
                •  delete from:指定要删除的表;
                • where:可选句子,用于指定删除的条件。如果不使用where句子,那么将删除所有数据。

                  示例:

                  --先查询语文成绩小于60的同学:
                  mysql> select * from exam where chinese  delete from exam where chinese select * from exam where chinese
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码