发表于 2025-1-15 19:38:18

在Mysql情况下对数据进行增编削查

一、插入数据:

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)   https://i-blog.csdnimg.cn/direct/96d6fd181f0e4794b9d4e75149ce2159.png   在门生表中插入“小明”数据的效果    二、修改数据:

update 表名 set 字段名1=新的数据值,字段名2=新的数据值 ;
UPDATE students SET name= '邓超';   https://i-blog.csdnimg.cn/direct/a98a8e3df0d04fc192e5e9b9d25cd1ac.png   将所有门生的姓名改成邓超的效果    三、删除数据:

1、delete from 表名;

----------删除表里的数据,但是表仍然存在
delete from 表名 ;------------根据条件进行删除表里的数据
DELETE FROM students where id = 1;   https://i-blog.csdnimg.cn/direct/44c75c60e4b048e3b46eb1d7375a41b8.png   delete from删除可以加条件    2、truncate table 表名;

---------清空表里的数据,但表仍然存在,而且不能加条件
TRUNCATE TABLE students where id = 1;   https://i-blog.csdnimg.cn/direct/d5ec709cb302440db65a68ffe8879ea0.png   truncate table删除数据会报错    四、数据查询:

1、根本查询:

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

select * from 表名;
select * from students;   https://i-blog.csdnimg.cn/direct/fe5fd415e0d041e48f6f1f0c4b56bbff.png   查询所有门生信息    ②查询部分字段的全部数据:

select 字段名1,字段名2......from 表名;
select name,gender from students;   https://i-blog.csdnimg.cn/direct/395419449b314c908b4ecde854fe299d.png   查询所有门生的姓名,性别    ③根据条件查询数据:

elect * from 表名 where 条件;
select name,gender from students where id = 1;   https://i-blog.csdnimg.cn/direct/66e76430b1ac44698af77ed4f62acbbd.png   查询id为1门生的姓名,性别    ④多个条件的查询:

条件与条件之间可以用and、or、in、between...and...来进行条件的毗连
select * from studentswheregender='女' andcls_id=2;   https://i-blog.csdnimg.cn/direct/a205177029164c38b5af11222abb751f.png   查询性别为女并且在2班的门生的信息    ⑤模糊查询:

select * from 表名 where 字段名 like '值';----------% _
select * from students where name like '小%';   https://i-blog.csdnimg.cn/direct/34d1f92516f54be693c62c0ed1468f02.png   查询名字里面包含'小'的门生的信息    ⑥去重:

select distinct 字段名 from 表名;
select distinct gender from students;   https://i-blog.csdnimg.cn/direct/a3c32358243c4d1298877c6b6064ea7a.png   查询性别有几种分类    ⑦排序:



[*]按照单个字段排序:
select * from 表名 order by 字段名 asc/desc;(asc升序-默认,desc降序)
select * from students order by height;   https://i-blog.csdnimg.cn/direct/90516cf1bd3647ec88e3ced886147abd.png   将门生的身高按照升序分列   

[*]按照多个字段排序:
select * from 表名 order by 字段名1 asc/desc,字段名2 asc/desc;
select * from students order by height,age;   https://i-blog.csdnimg.cn/direct/5c870b3576e24188b1c6f4b9f18ac54a.png   将门生的身高、年龄按照升序分列   

[*]有条件的排序:
select * from 表名 where 条件 order by 字段名 asc/desc;
select * from students where age = 18 order by height;   https://i-blog.csdnimg.cn/direct/c77e20ed1c5f43a4a086a731bc08740e.png   将年龄为18岁的门生按照身高升序分列    ⑧限制查询结果的数目:

limit
select * from students limit 2;   https://i-blog.csdnimg.cn/direct/67343e0f444945b18b91581724be2ce2.png   只看前2条门生信息    2、毗连查询:

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



[*]显示内毗连:select 字段 from 表名1 inner join 表名2 on 两个表毗连的条件 ;
select s.name,c.name from students s inner join classes c on s.cls_id=c.id;   https://i-blog.csdnimg.cn/direct/e9fdb039a08b46d692cea60e3d2096f7.png   查看门生所在班级   

[*]隐式内毗连:select 字段 from 表名1,表名2 where 两个表毗连的条件 ;
select s.name as '名字',c.name as '班级' from students s, classes c where s.cls_id = c.id;   https://i-blog.csdnimg.cn/direct/101ffbc5c5f54c5ca24228e418579d75.png   查看门生所在班级    ②外毗连



[*]左外毗连:select 字段 from 表名1 left join 表名2 on 两个表毗连的条件 ;------左表的数据全部查询出来,右表符合条件的查询出来
select c.name,t.name from classes c left join teachers t on c.teacher_id = t.id;   https://i-blog.csdnimg.cn/direct/01b788a65e3b4baa859ea161cacd96dc.png   查看老师所在班级   

[*]右外毗连:select 字段 from 表名1 right join 表名2 on 两个表毗连的条件 ;------右表的数据全部查询出来,左表符合条件的查询出来
select c.name,t.name from classe c right join teachers t on c.teacher_id = t.id;   https://i-blog.csdnimg.cn/direct/9a9ebf55282f4112af83840eed8961b0.png   查看老师所在班级    3、聚合函数查询:

①count()-计数

select count(*) as '学生总数' from students;   https://i-blog.csdnimg.cn/direct/6ca930ffdd2f41f0a30fbaa13f7ce7d7.png   查询班级有多少同砚    ②sum()-求和

select sum(height) as '身高之和' from students;   https://i-blog.csdnimg.cn/direct/b1aacf8edbd44d54999a169e1680aedf.png   查询班级门生的身高之和    ③max()-最大值

select max(height) as '最高身高' from students;   https://i-blog.csdnimg.cn/direct/650949d1dbf0474f95572aca534738b9.png   查询班级门生的最高身高    ④min()-最小值

mysql> select min(height) as '最矮身高' from students;   https://i-blog.csdnimg.cn/direct/a580524263c2494f8d5130b085b1ab45.png   查询班级门生的最矮身高    ⑤avg()-平均值

select avg(height) as '平均身高' from students;   https://i-blog.csdnimg.cn/direct/434bc2401b9a43d89d5ca1f3265e1966.png   查询班级门生的平均身高    ⑥select 聚合函数名(字段名) from 表名 ;

SELECT AVG(height) AS '1班平均身高' FROM students WHERE cls_id = 1;   https://i-blog.csdnimg.cn/direct/d097c019ad6145b6a530b33ee2f8b784.png   查询1班门生的平均身高    ⑦select 分组的字段名,聚合函数名(字段名) from 表名 ;

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;   https://i-blog.csdnimg.cn/direct/23493efa079b41fc9c04d669512e1236.png   按班级分组查询每个班级的门生人数、平均年龄、最高身高和最低身高    4、子查询:查询嵌套查询

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

select * from 表名 where 字段名=(select 字段名 from 表名);
select * from students where cls_id = (select cls_id from students where name = '刘德华');   https://i-blog.csdnimg.cn/direct/6a6345a179444795bc1dd587321cf6d9.png   查看刘德华同砚的所在班级的所有同砚    ②子查询的结果有多个值,等于其中的恣意一个值

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='赵老师'));   https://i-blog.csdnimg.cn/direct/ef3b23e408a942c69d741865848ded3f.png   查看赵老师所带的门生信息    ③子查询的结果有多个值,大于所有值

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='赵老师'));   https://i-blog.csdnimg.cn/direct/0cc4d4fc781e46a8982894e6d75f78a0.png   查看门生所在班级    ④子查询如果有查询的结果,外查询就实行

select * from 表名 where exists (select 字段名 from 表名);
select * from classes where exists (select * from teachers where name='李老师');   https://i-blog.csdnimg.cn/direct/166ed94913eb421d91cf8bddd80a8a06.png   查看存在李老师的班级表   
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 在Mysql情况下对数据进行增编削查