MySQL的学习

MySQL的学习

MySQL的增删改查

  1. 查询语句语法

    查询可以时一个结果集,是一个 列表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    -- 查询mytable列表中的所有数据
    select * from mytable

    -- 查询mytable列表中的id和name字段
    select id,name from mytable

    -- 带条件的查询列表
    select * from mytable where id = 3
    select * from mytable where age < 20

    -- 创建多条件查询(not>! and>&& or>||)
    select * from mytable where id = 3 and age < 20
  2. 增加语句语法

    insert into 表名(字段1,字段2,…) values(值1,值2,…)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    -- 如果表名没有指定字段那么必须设置对应的数量的值
    insert into mytable values('lili',30,0) --因为没有指定字段,没有设置对应数量的值所以会报错

    -- 如果有标识列,那么标识列一般可以给null值,如果给null值系统就会自动生成标识
    insert into mytable values(null,'lili',30,0)

    -- 指定多要添加的字段赋值
    insert into mytable(name,age,gender) values('lili',30,0)

    -- 对于可以为null的字段可以不赋值
    insert into mytable(name) values('dc')

    -- 非空字段须要赋值,否则不会自动生成默认值
    insert into mytable(age,gender) values('dc',18) --虽然可以但是不建议
  3. 修改语句语法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    -- 修改语法 update 表名 set 字段1 = 值1, 字段2 = 值2... where 条件
    update mytable set age = age + 1 -- 没有赋予条件会把所有的age字段的所有值都改变

    -- 带条件的修改语法只会修改条件范围内的值
    update mytable set age = age + 1 where id = 5

    -- 同样支持not and or

    -- 修改多个内容
    update mytable set age = age +1,gender = 1 where id = 5
  4. 删除语句语法

    1
    2
    3
    4
    5
    6
    -- 删除语句的语法 delete [from] 表名 where 条件
    -- 删除的操作不能还原,要删除的话,就需要提前备份
    delete from mytable where id = 8

    -- 同时删除多个
    delete from mytable where id in(4,5)

常见的函数说明

  1. 总条数 count

    1
    2
    3
    4
    5
    6
    7
    -- 查询可以满足条件的记录数
    select count(*) from mytable

    -- 选择符合条件的记录数
    select count(id) from mytable

    -- 如果当前空值,不会对null值进行计算
  2. 最大值、最小值 max min

    1
    2
    3
    4
    -- max 获取最大值  min  获取最小值
    select max(age) from mytable

    -- 如果是字符,按照字符的ascll码来排序
  3. 平均值 avg

    1
    2
    -- 一般都是数值
    select svg(age) from mytable
  4. 排序 order by

    select * | 字段列表 form 表列表 where order by asc 升序 desc 降序

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    select * from mytable

    -- 降序
    select * from mytable order by id desc

    -- 按照name排序 a-z
    select * from mytable order by name

    -- null值会排在前面

    -- 实现按照性别,再按照年龄
    select * from mytable order by gender,age
  5. 获取指定范围内的数据

    limit 获取指定的前n条记录 只有一个参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    -- 前面3条数据
    select * from mytable limit 3

    -- 后面五条
    -- 先做降序,然后再去筛选,并且一定要先排序,再获取,不然会报错
    select * from mytable order by id desc limit 5

    -- 中间范围的记录 n 偏移量从0开始, m 能够获取的记录数
    select * from mytable limit 2,2

    -- 第2种写法,和上面的一样
    select * from mytable limit 4 offset 2

多表查询

在数据中,防止重复存储数据,所以会把不同的数据放在不同的地方存储

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-- 返回初始的数据,没有内部关联的数据
select * from student

-- 用户需要的是最终的结果
-- 1.0 采用from where的方式
select * from student,class where student.cid = class.classid -- where后面的这个 = 表示判断

-- 简写
select studentId,studentName,age,gender,className from student,class where student.cid = class.classid

-- 2.0 join 和 inner join都是一样的 on和where的意思也是差不多的
select * from student inner join class on student.cid = class.classid


-- left join 如果对应不上的时候,自动让对应的值为空 right join 与之相反
select * from student left join class on student.cid = class.classid

文章目录
  1. 1. MySQL的学习
    1. 1.1. MySQL的增删改查
    2. 1.2. 常见的函数说明
    3. 1.3. 多表查询
|