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

  金牌会员 | 2025-1-15 19:38:18 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 989|帖子 989|积分 2967

一、插入数据:

insert into 表名 [(字段名)] values (字段对应的值1,字段对应的值2,.......)[,(字段对应的值1,字段对应的值2,.......)];
  1. insert into students (id,name,age,height,gender,cls_id,is_delete)
  2. values (0,'小明',18,180.00,2,1,0)
复制代码
  
     在门生表中插入“小明”数据的效果    二、修改数据:

update 表名 set 字段名1=新的数据值,字段名2=新的数据值 [where 条件];
  1. UPDATE students SET name= '邓超';
复制代码
  
     将所有门生的姓名改成邓超的效果    三、删除数据:

1、delete from 表名;

----------删除表里的数据,但是表仍然存在
delete from 表名 [where 条件];------------根据条件进行删除表里的数据
  1. DELETE FROM students where id = 1;
复制代码
  
     delete from删除可以加条件    2、truncate table 表名;

---------清空表里的数据,但表仍然存在,而且不能加条件
  1. TRUNCATE TABLE students where id = 1;
复制代码
  
     truncate table删除数据会报错    四、数据查询:

1、根本查询:

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

select * from 表名;
  1. select * from students;
复制代码
  
     查询所有门生信息    ②查询部分字段的全部数据:

select 字段名1,字段名2......from 表名;
  1. select name,gender from students;
复制代码
  
     查询所有门生的姓名,性别    ③根据条件查询数据:

elect * from 表名 where 条件;
  1. select name,gender from students where id = 1;
复制代码
  
     查询id为1门生的姓名,性别    ④多个条件的查询:

条件与条件之间可以用and、or、in、between...and...来进行条件的毗连
  1. select * from students  where  gender='女' and  cls_id=2;
复制代码
  
     查询性别为女并且在2班的门生的信息    ⑤模糊查询:

select * from 表名 where 字段名 like '值';----------% _
  1. select * from students where name like '小%';
复制代码
  
     查询名字里面包含'小'的门生的信息    ⑥去重:

select distinct 字段名 from 表名;
  1. select distinct gender from students;
复制代码
  
     查询性别有几种分类    ⑦排序:



  • 按照单个字段排序:
select * from 表名 order by 字段名 asc/desc;(asc升序-默认,desc降序)
  1. select * from students order by height;
复制代码
  
     将门生的身高按照升序分列   

  • 按照多个字段排序:
select * from 表名 order by 字段名1 asc/desc,字段名2 asc/desc;
  1. select * from students order by height,age;
复制代码
  
     将门生的身高、年龄按照升序分列   

  • 有条件的排序:
select * from 表名 where 条件 order by 字段名 asc/desc;
  1. select * from students where age = 18 order by height;
复制代码
  
     将年龄为18岁的门生按照身高升序分列    ⑧限制查询结果的数目:

limit
  1. select * from students limit 2;
复制代码
  
     只看前2条门生信息    2、毗连查询:

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



  • 显示内毗连:select 字段 from 表名1 inner join 表名2 on 两个表毗连的条件 [where 条件];
  1. select s.name,c.name from students s inner join classes c on s.cls_id=c.id;
复制代码
  
     查看门生所在班级   

  • 隐式内毗连:select 字段 from 表名1,表名2 where 两个表毗连的条件 [and 其他查询的条件];
  1. 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 条件];------左表的数据全部查询出来,右表符合条件的查询出来
  1. 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 条件];------右表的数据全部查询出来,左表符合条件的查询出来
  1. select c.name,t.name from classe c right join teachers t on c.teacher_id = t.id;
复制代码
  
     查看老师所在班级    3、聚合函数查询:

①count()-计数

  1. select count(*) as '学生总数' from students;
复制代码
  
     查询班级有多少同砚    ②sum()-求和

  1. select sum(height) as '身高之和' from students;
复制代码
  
     查询班级门生的身高之和    ③max()-最大值

  1. select max(height) as '最高身高' from students;
复制代码
  
     查询班级门生的最高身高    ④min()-最小值

  1. mysql> select min(height) as '最矮身高' from students;
复制代码
  
     查询班级门生的最矮身高    ⑤avg()-平均值

  1. select avg(height) as '平均身高' from students;
复制代码
  
     查询班级门生的平均身高    ⑥select 聚合函数名(字段名) from 表名 [where 条件];

  1. SELECT AVG(height) AS '1班平均身高' FROM students WHERE cls_id = 1;
复制代码
  
     查询1班门生的平均身高    ⑦select 分组的字段名,聚合函数名(字段名) from 表名 [group by 分组的字段名];

  1. 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 表名);
  1. select * from students where cls_id = (select cls_id from students where name = '刘德华');
复制代码
  
     查看刘德华同砚的所在班级的所有同砚    ②子查询的结果有多个值,等于其中的恣意一个值

select * from 表名 where 字段名=any(select 字段名 from 表名);
  1. 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 表名);
  1. 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 表名);
  1. select * from classes where exists (select * from teachers where name='李老师');
复制代码
  
     查看存在李老师的班级表   
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表