• mysql数据的基本操作



    本文内容:

    • 插入数据:
    • 查询数据
    • 修改数据
    • 删除数据

     首发日期:2018-04-11


    插入数据:

    • 给所有字段插入数据:
      • 插入单条记录:insert into 表名 values(值列表);
      • 插入多条记录:insert into 表名 values(值列表),(值列表),(值列表)…;
    • 给某些字段插入数据(与上面一样有单条和多条的用法):insert into 表名(字段列表) values(值列表);
    use test;
    create table student(
    id int primary key auto_increment,
    name varchar(15) not null,
    gender varchar(10)
    );
    -- 插入数据
    insert into student(name,gender) values("lilei","male");
    insert into student(name,gender) values("jack","male"),('alice','female');
    -- 查询
    select * from student;

    查看数据:

    • 查看所有字段:
      • select * from 表名;
      • select 所有字段 from 表名;
    • 查看部分字段:
      • select 字段列表 from 表名;
    • 根据条件筛选结果:where子句
      • select 字段列表 from 表名 where 条件;

    补充:

    • where子句:
      • 基于值:
        • = : where 字段 =值  ;查找出对应字段等于对应值的记录。(相似的,<是小于对应值,<=是小于等于对应值,>是大于对应值,>=是大于等于对应值,!=是不等于)
        • like: where 字段 like '模糊匹配',like的功能与 = 相似 ,但可以使用模糊匹配来查找结果。
      • 基于值的范围:
        • in: where 字段 in 范围;查找出对应字段的值在所指定范围的记录。
        • not in : where 字段 not in 范围;查找出对应字段的值不在所指定范围的记录。
        • between x and y :查找出对应字段的值在闭区间[x,y]范围的记录。
      • 条件复合:
        • or : where 条件1 or 条件2… ; 查找出符合条件1或符合条件2的记录。
        • and:  where 条件1 and 条件2… ; 查找出符合条件1并且符合条件2的记录。
        • not : where not 条件1 ;查找出不符合条件的所有记录。
        • &&的功能与and相同;||与or功能类似,!与not 功能类似。
        select * from student;
        select name from student;
        
        select * from student where name ="lilei";
        select * from student where id != 1;
        select * from student where name like "li%";
        
        select * from student where id in (1,3,5); -- 值为1,3,5的记录
        select * from student where id not in (1,3,5);
        select * from student where  id  between 1 and 5;
        
        select * from student where name ="lilei" or name ="jack";
        select * from student where not id =1;
    • 这里只是简单的select语句,想了解更多可以参考我的另外一篇博文(超链接->:mysql学习之完整的select语句)。

    更新数据:

    • 更新全部记录:
      • update 表名 set  字段名 = 值;
    • 更新多个字段:
      • update 表名 set 字段名 =值,字段名=值,…;
    • 更新指定记录:
      • update 表名 set 字段名 = 值 where 条件; 【where条件参考上面的select的补充内容】
    • 更新指定条数记录:
      • update 表名 set 字段名 = 值 where 条件 limit count;【count是指定更新条数】
    update student set name = "lilei";
    update student set name = "hanmeimei",gender ="female" where name="lilei" limit 1;
    update student set name = "lile" where id =2;
    update student set name = "lile" where name = "lilei" limit 2;

    修改数据:

    • 删除所有记录:
      • delete from 表名 ; 【慎用!】
    • 删除指定记录:
      • delete from 表名 where 条件;【where条件参考上面的select的补充内容】
    • 删除指定条数:
      • delete from 表名 where 条件 limit count;【count是指定更新条数】
    delete from student where id =2;

    补充:

    • 删除需谨慎。


  • 相关阅读:
    vue2.0 练习项目-外卖APP(2)
    vue2.0 练习项目-外卖APP(1)
    迈向全栈开发学习(3)
    迈向全栈开发学习(2)
    迈向全栈开发学习(1)
    前端到后台ThinkPHP开发整站(完)
    前端到后台ThinkPHP开发整站(7)
    前端到后台ThinkPHP开发整站(6)
    前端到后台ThinkPHP开发整站(5)
    uniapp 设置全局scss生效
  • 原文地址:https://www.cnblogs.com/progor/p/8786167.html
Copyright © 2020-2023  润新知