当前位置: 代码网 > it编程>数据库>Mysql > 在Mysql环境下对数据进行增删改查的操作方法

在Mysql环境下对数据进行增删改查的操作方法

2025年02月09日 Mysql 我要评论
一、插入数据:insert into 表名 [(字段名)] values (字段对应的值1,字段对应的值2,…)[,(字段对应的值1,字段对应的值2,…)];insert

一、插入数据:

insert into 表名 [(字段名)] values (字段对应的值1,字段对应的值2,…)[,(字段对应的值1,字段对应的值2,…)];

insert into students (id,name,age,height,gender,cls_id,is_delete)
values (0,'小明',18,180.00,2,1,0)

在学生表中插入“小明”数据的效果

二、修改数据:

update 表名 set 字段名1=新的数据值,字段名2=新的数据值 [where 条件];

update students set name= '邓超';

将所有学生的姓名改成邓超的效果

三、删除数据:

1、delete from 表名;

----------删除表里的数据,但是表仍然存在

delete from 表名 [where 条件];------------根据条件进行删除表里的数据

delete from students where id = 1;

delete from删除可以加条件

2、truncate table 表名;

---------清空表里的数据,但表仍然存在,而且不能加条件

truncate table students where id = 1;

truncate table删除数据会报错

四、数据查询:

1、基本查询:

①查询全部字段的全部数据:

select * from 表名;

select * from students;

查询所有学生信息

②查询部分字段的全部数据:

select 字段名1,字段名2…from 表名;

select name,gender from students;

查询所有学生的姓名,性别

③根据条件查询数据:

elect * from 表名 where 条件;

select name,gender from students where id = 1;

查询id为1学生的姓名,性别

④多个条件的查询:

条件与条件之间可以用and、or、in、between…and…来进行条件的连接

select * from students  where  gender='女' and  cls_id=2;

查询性别为女并且在2班的学生的信息

⑤模糊查询:

select * from 表名 where 字段名 like ‘值’;----------% _

select * from students where name like '小%';

查询名字里面包含’小’的学生的信息

⑥去重:

select distinct 字段名 from 表名;

select distinct gender from students;

查询性别有几种分类

⑦排序: 按照单个字段排序:

select * from 表名 order by 字段名 asc/desc;(asc升序-默认,desc降序)

select * from students order by height;

将学生的身高按照升序排列

按照多个字段排序:

select * from 表名 order by 字段名1 asc/desc,字段名2 asc/desc;

select * from students order by height,age;

将学生的身高、年龄按照升序排列

有条件的排序:

select * from 表名 where 条件 order by 字段名 asc/desc;

select * from students where age = 18 order by height;

将年龄为18岁的学生按照身高升序排列

⑧限制查询结果的数量:

limit

select * from students limit 2;

只看前2条学生信息

2、连接查询:

(涉及到两个表以上,在查询的时候至少要有一个必备的连接条件,这个必备的条件就是两个表共有的那个字段相等,而且这个字段一定在一个表里是主键,在另一个表里是外健)

①内连接

显示内连接:select 字段 from 表名1 inner join 表名2 on 两个表连接的条件 [where 条件];

select s.name,c.name from students s inner join classes c on s.cls_id=c.id;

查看学生所在班级

隐式内连接:select 字段 from 表名1,表名2 where 两个表连接的条件 [and 其他查询的条件];

select s.name as ‘名字',c.name as ‘班级' from students s, classes c where s.cls_id = c.id;

查看学生所在班级

②外连接

左外连接:select 字段 from 表名1 left join 表名2 on 两个表连接的条件 [where 条件];------左表的数据全部查询出来,右表符合条件的查询出来

select c.name,t.name from classes c left join teachers t on c.teacher_id = t.id;

查看老师所在班级

右外连接:select 字段 from 表名1 right join 表名2 on 两个表连接的条件 [where 条件];------右表的数据全部查询出来,左表符合条件的查询出来

select c.name,t.name from classe c right join teachers t on c.teacher_id = t.id;

查看老师所在班级

3、聚合函数查询:

①count()-计数

select count(*) as '学生总数' from students;

查询班级有多少同学

②sum()-求和

select sum(height) as '身高之和' from students;

查询班级学生的身高之和

③max()-最大值

select max(height) as '最高身高' from students;

查询班级学生的最高身高

④min()-最小值

mysql> select min(height) as '最矮身高' from students;

查询班级学生的最矮身高

⑤avg()-平均值

select avg(height) as '平均身高' from students;

查询班级学生的平均身高

⑥select 聚合函数名(字段名) from 表名 [where 条件];

select avg(height) as '1班平均身高' from students where cls_id = 1;

查询1班学生的平均身高

⑦select 分组的字段名,聚合函数名(字段名) from 表名 [group by 分组的字段名];

select cls_id as class_id, count(*) as student_count, avg(age) as average_age, max(height) as max_height, min(height) as min_height from students group by cls_id;

按班级分组查询每个班级的学生人数、平均年龄、最高身高和最低身高

4、子查询:

查询嵌套查询

①子查询的结果只有一个值

select * from 表名 where 字段名=(select 字段名 from 表名);

select * from students where cls_id = (select cls_id from students where name = '刘德华');

查看刘德华同学的所在班级的所有同学

②子查询的结果有多个值,等于其中的任意一个值

select * from 表名 where 字段名=any(select 字段名 from 表名);

select * from students where cls_id = any(select id from classes where teacher_id = (select id from teachers where name='赵老师'));

查看赵老师所带的学生信息

③子查询的结果有多个值,大于所有值

select * from 表名 where 字段名>all(select 字段名 from 表名);

select * from students where cls_id >= all(select id from classes where teacher_id = (select id from teachers where name='赵老师'));

查看学生所在班级

④子查询如果有查询的结果,外查询就执行

select * from 表名 where exists (select 字段名 from 表名);

select * from classes where exists (select * from teachers where name='李老师');

查看存在李老师的班级表

到此这篇关于在mysql环境下对数据进行增删改查的文章就介绍到这了,更多相关mysql数据增删改查内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

  • docker拉取MySQL8.0镜像以及安装的完整步骤

    一、docker安装mysql镜像搜索imagesdocker search mysql 我配置了阿里云镜像加速器,找不到mysql镜像.由于不知道名原因很多国内拉取镜像都下架了d…

    2025年02月08日 数据库
  • MySQL之锁类型解读

    mysql锁类型解读锁的分类图,如下:锁操作类型划分读锁 : 也称为共享锁 、英文用s表示。针对同一份数据,多个事务的读操作可以同时进行而不会互相影响,相互不阻塞的。写锁 : 也称…

    2025年02月08日 数据库
  • MySQL分批插入/更新数据方式

    MySQL分批插入/更新数据方式

    mysql分批插入/更新数据在我们的日常开发中,经常会使用到批量insert/update的语句来实现相关的业务功能。而如果数据量比较大的话,会导致sql语句更... [阅读全文]
  • 生产环境的MySQL事务隔离级别方式

    生产环境的MySQL事务隔离级别方式

    生产环境的mysql事务隔离级别mysql 数据库的默认隔离级别是 rr( 可重复读 ),但是很多大公司把隔离级别改成了 rc(读已提交),主要原因是为了提高并... [阅读全文]
  • MySql9.1.0安装详细教程(最新推荐)

    mysql介绍:‌mysql是一个流行的关系型数据库管理系统,由瑞典mysql ab公司开发,目前属于oracle旗下产品。‌ mysql以其体积小、速度快、总…

    2025年02月08日 数据库
  • Mysql分库分表实现方式

    Mysql分库分表实现方式

    1、背景mysql作为做流行的开源数据库,在各大互联网公司被广泛使用。通常我们用一个库就可以满足需求,但是随着业务的增长,数据量和并发量迅速膨胀。mysql单表... [阅读全文]

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com