mysql数据的操作Day13

MySQL的数据的操作

数据的增删改查的单词

**增:**insert into

**删:**delete

**改:**update

**查:**select

一 INSERT 数据的添加

主体结构:

指定字段添加值:

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

不指定字段添加值:(全部添加值)

insert into 表名 values()

添加多个值:

insert into 表名[(指定的字段)] values(值),(值)…

快速插入值:

insert into 表名[(字段名)] select 字段[,*] from 表名

insert into a(username) select username from a;

insert into a select * from a;

注意:字段的值和字段的名是一一对应的

二 SELECT 数据的查询

主体结构:

select 字段名/* from 表名

(1) 查询所有的数据(不建议)

select * from 表名

(2) 指定字段进行查询(建议)

select 字段名 from 表名

给字段起别名:

select 字段名 别名 from 表名

关键字 as

select 字段名 as 别名 from 表名

三 WHERE 条件

(1) 比较运算符
  1. >

    select * from 表名 where id>20

  2. <

    select * from 表名 where id<20

  3. >=

    select * from 表名 where id>=20

  4. \<=

    select * from 表名 where id<=20

  5. =

    select * from 表名 where id=20

  6. !=/<>

    select * from 表名 where id!=20

    select * from 表名 where id<>20

(2) 逻辑运算符
  1. and 并且

    select * from a where age>18 and age!=32; 查询所有条件是 年龄18以上但是不要32的

  2. or 或

    select * from a where age<=20 or id<20;

  3. between and 在….之间

    select * from a where age between 18 and 20;

    select * from a where age>=18 and age<=20;

  4. not between and 不在….之间

    select * from a where age not between 18 and 20;

    select * from a where age\<18 or age>20;

  5. in 在….里面

    select * from a where age in(18,20);

    select * from a where age=18 or age=20;

(3) 子查询(条件还是一条sql语句)

select * from a where id in(select id from a where age>18)

(4) order by 排序(默认是升序)

order by 字段名 asc/desc 升序/降序

select * from a order by age desc;

select * from a where id>40 order by id desc;

(5) is not is 用来查询某个字段是否为null
  1. 查询username为null的数据

    select * from a where username is null;

  2. 查询username不为null的数据

    select * from a where username is not null;

(6) limit 取值

从开头位置取出5条数据

limit 5 取出5条数据

select * from a limit 5;

等同于

limit 0,5

select * from a limit 0,5;

条件组合式的查询

select * from a where age>=30 order by id desc limit 5; 查询年龄大于等于30的 按照id降序排序 取出id最大的5条数据

(7) group by 分组
统计 男和女 分别有多少人

select sex,count(*) as total from a group by sex;

统计 每个班级的男和女分别有多少人

select sex,classid,count(*) as total from a group by sex,classid;

select classid,sex,count(*) from a group by classid,sex;

having 条件 相当于你的 where

按照班级 性别来分组 查询每班人数大于2人的数据

select classid,sex,count(*) as total from a group by classid,sex having total>2;

按照班级 性别来分组 查询 python1708的数据

select classid,sex,count(*) as total from a group by classid,sex having classid=’python1708’;

select classid,sex,count(*) as total from a group by classid,sex having classid in(‘python1707’,’python1708’) and total>2;

(8) 模糊查询 like

主体结构:

like ‘%字符%’ 包含

select * from a where username like ‘%三%’

like ‘%字符’ 以某个字符结尾的数据

select * from a where username like ‘%四’;

like ‘字符%’ 以某个字符开头的数据

select * from a where username like ‘四%’;

(9) DISTINCT 去除重复的值

select distinct age from a; 查询去除重复的数据后的age字段的值

四 DELETE 删除

主体结构:

delete from 表名 [where 条件]

select * from a where id=2;

清空表的方式
  1. truncate 表名 自增回归原位

  2. delete from 表名 删除表名但自增不为1

    alter table 表名 auto_increment=1 把自增设置为1

注意:
  1. 删除的时候 如果没有条件 删除所有的数据删除后的数据 自增依然记录当前的数据的位置

五 UPDATE 修改

主体结构:

update 表名 set 字段名1=值1[,字段名2=值2 where 条件]

update a set username=’三四张’,sex=’m’ where id=1;

在所有的年龄的基础上 加2

update a set age=age+2;

注意:

当修改的时候 没有 where条件的时候 会将所有的 数据 都进行修改

六 聚合函数

  1. count() 统计个数
  2. max() 最大值
  3. min() 最小值
  4. sum() 求和
  5. avg() 求平均数

七 多表联查

(1) 隐式内连接查询

select * from goods,user where user.id = goods.uid and uid=1
select user.id,user.username,goods.goodsname from goods,user where user.id = goods.uid and uid=1

(2) 显式内连接查询 INNER JOIN

select * from user INNER JOIN goods on user.id=goods.uid and user.id=2

A表 inner join B表 on 条件

(3) 左关联 LEFT JOIN

select * from user LEFTJOIN goods on user.id=goods.uid and user.id=2

注意:

左关联以左表为主表 右表为辅表 会将主表所有的数据查询出来 辅表没有关联匹配上的数据 用null来占位

(4)右关联 RIGHT JOIN

select * from user RIGHT JOIN goods on user.id=goods.uid and user.id=2

注意:

左关联以右表为主表 左表为辅表 会将主表所有的数据查询出来 辅表没有关联匹配上的数据 用null来占位

八 以下代码作为了解

(1) 修改密码

set password for 用户名@localhost = password(‘新密码’)

(2) 创建用户
  1. 选择mysql库

    use mysql

  2. 查看所有的用户

    select user from user

  3. 创建用户

    create user lisi identified by ‘123456’;

  4. 授予权限

    grant all on 库名.* to 用户名

    all 代表所有权限

    select,update

  5. 回收权限

    revoke all on 库名.* from 用户名

  6. 删除用户

    drop user 用户名

  7. 刷新

    flush privileges

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值