zoukankan      html  css  js  c++  java
  • MySQL四大秘笈之增删改查

    MySQL四大秘笈之增删改查

    MySQL四大秘技之增删改查

    写在1前面: 举例的例全部用了栗子的栗是故意的,手动滑稽

    秘技一:增

    • 为表的指定字段添加数据: insert into 表名 (指定的字段名一,指定的字段名2....) values(对应字段名一的值,对应字段名二的值....)
    • 举个栗子: insert into tb_user(id,account,password) values (1,'张三',zhangsan)
    • 不指定字段添加数据: insert into 表名 values(值一,值二,值三....) 因为没有指定字段,所以这里的值要跟数据库字段的顺序保持一致
    • 再举个栗子: insert into tb_user values(2,'李四',lisi,18)
    • 同时插入多条数据的时候,指定字段的行为只进行一次insert into 表名 (指定字段) values (第一条值),(第二条值),(第三条值),(....)
    • 依然是举个栗子: insert into tb_user(id,account,password) values (3,'王五',wangwu),(4,'赵六',zhaoliu),(5,'小明',xiaoming)

    秘技二:删

    • 删除语法: delete from 表名 where 条件
    • 删除部分数据,需要使用限制条件where
      • 举个栗子: 删除user表中小明的数据,已知,小明id为5
      • delete from tb_uesr where id = 5
    • 全部删除
      • 语法: truncte [table] 表名
      • 举栗子: truncate table tb_user
      • ps: 使用truncate的时候,如果id是自增的,就会从删除前最大的条数+1开始记录,不会重新归到0

    秘技三:改

    • MySQL中没有直接说明对数据进行更改,所谓的更改则是,更新数据(其实没什么区别)
    • 更新数据指的是对已有数据进行修改
    • 更新数据语法: update 表名 set 字段名1 = 值1,字段名2 = 值2[,字段名N = 值N][,where 条件表达式]
    • 举个栗子: 已知李四字段拥有年龄,但是年龄数值不对,需要修改
    • update tb_uesr set age = 20 where id = 2

    秘技四:查

    • 查询是MySQL中最常用的也是最基本的
    • 语法结构: select 字段 from 表 where 限制条件
    • 基本查找
      • 查询所有字段; select * from tb_user
      • 根据id查询某一人的全部字段: select * from tb_uesr where id = 1
      • 根据需要的字段进行查询,比如要查询id和账户: select id,account from tb_user [where......]
    • 按条件查找
      • where后面可接关系运算符'=' , '< >' , ' != ' , '<' , '>' , '<=' , '>='
      • 带in关键字的查询,可以用来判断某个字段的值是否在指定集合中: where 字段 [not] in (元素1,,元素2,....)
      • 举个栗子: select * from tb_user where id in(1,2,3,4)
      •  
      • 带between and关键字的查询,可以判断字段的值是否在规定的两个值的范围内
      • 语法: where 字段 [not] between 值1 and 值2
      • 按照惯例,举个大栗子: select * from tb_uesr where id between 2 and 4
      •  
      • 过滤重复值
      • 语法: select distinct 字段名 from 表名
      • 举例子: select distinct age from tb_user
      •  
      • 空值查询
      • 在数据表中,有些值可能为空值null,空值不等同于0,也不同于空字符串,需要使用is null来判断字段的值是否为空
      • 语法: select * from 表名 where 字段名 is [not] null
      • 查询age为空的数据
      • 栗子: select * from tb_user where age is null
      •  
      • 模糊查询like
      • 语法; where 字段名 [not] like '字符串 可能含有通配符'
      • 举个栗子: select * from tb_user where account like '%ang%'
      • 通配符
        • 百分号通配符(%): 匹配任意长度字符串
        • 下划线通配符(%): 下划线只能匹配单个字符
         
      • 带and的多条件查询
      • 语法: where 条件一 and 条件二 and 条件N
      • 举栗: select * from tb_user where account = '张三' and password = zhangsan
      •  
      • 带or的多条件查询
      • 语法: where 条件一 or 条件二 or 条件N
      • 举栗: select * from tb_user where account = '张三' or account = '李四'
    • 聚合函数
      • count: 返回条数
      • 语法: select count(*) from 表名
      • count(这里可以选择通配符*也可以单独指定字段)
      •  
      • sum: 求和
      • 求出某个字段所有值的综合
      • 语法: select sum(字段名) from 表名
      • 举栗: 求所有人年龄总和 select sum(age) from tb_user
      •  
      • avg: 求平均值
      • 语法:select avg(字段) from 表名
      • 举栗: select avg(age) from tb_user
      •  
      • max: 最大值
      • 语法:select max(字段) from 表名
      • 举栗: select max(age) from tb_user
      •  
      • min: 最小值
      • 语法:select min(字段) from 表名
      • 举栗: select min(age) from tb_user
    • 排序
      • 语法: select 字段 from 表名 order by 字段一[asc | desc],字段N
      • asc升序,默认为升序,可不写
      • desc,降序
    • 分组查询
      • 分组查询可以使用聚合函数
      • 语法: select 字段名和聚合函数 from 表名 group by 字段 [having 限制条件]
      • 举栗一: 按年龄分组select age from tb_user group by age
      • 举栗二: 使用聚合函数,首先添加字段性别alter table tb_user gender varchar(2),然后插入数据inster into (gender) values('男') where id = 1 or id = 3-----接着开始分组查询,统计人数,按照性别分组select count(id),gender from tb_user group by gender
    • 分页查询
      • 语法: select 字段 from 表名 limit 开始行,页大小
      • 举栗: select * from tb_user limit 0,2
      • 页码开始行计算公式: (页码-1) * 页码大小
    • 取别名
      • 字段 as 别名
      • 字段 别名
      • 字段 = 别名
    如有问题,请发送邮件至buxiaqingcheng@163.com或者buxiaqingcheng@dingtalk.com
  • 相关阅读:
    [JSOI2016]最佳团体
    CF125E MST Company
    CF482C Game with Strings
    CF379F New Year Tree
    CF1051F The Shortest Statement
    小a和uim之大逃离
    新魔法药水
    翻硬币
    [CQOI2017]小Q的棋盘
    UVA11729突击战
  • 原文地址:https://www.cnblogs.com/zhenzhunaichabujiatang/p/13067009.html
Copyright © 2011-2022 走看看