MySQL中的增查操作:探索数据的奥秘,开启数据之门
本节,我们继承深入相识MySQL,本章所讲的基础操作,针对的是表的增删查改!一、Create 新增
1.1、语法
INSERT table_name
[(column [, column] ...)]
VALUES
(value_list) [, (value_list)] ...
value_list: value, [, value] ... 1.2、示例
向 student 表中新增喜羊羊及其id;
#单行数据全列插入
insert into student values (1,'喜羊羊');
#单行数据指定列插入
insert into student (name) values ('懒洋洋');
insert into student (id,name) values (3,'小灰灰');
#多行数据指定列插入
insert into student (id,name) values (4,'沸羊羊'),(5,'慢羊羊'); (1) 单行数据全列插入(如喜羊羊的插入)
(2) 单行数据指定列插入(如懒洋洋的插入,未设置默认值是,默以为NULL)
(3) 多行数据指定列插入(如沸羊羊和慢羊羊的插入)
由上述示例可知:插入一条新数据行时,字段名处可以是一个大概是多个,后面括号内应按照字段名的顺序,设置对应的值,若有字符串类型的值,应使用英文单引号包裹
https://i-blog.csdnimg.cn/direct/9a3cbf682848436bbdda1e4c5fc557bf.png
https://i-blog.csdnimg.cn/direct/7025324904af4b1186860131e12a56e0.png
如果,我们插入的值与前面字段名顺序或数据类型不匹配时,会发生什么?
insert into student (id,name) values ('测试','无名氏');
insert into student (id,name) values ('测试','7'); https://i-blog.csdnimg.cn/direct/35767834681042ac872d13ebd5c66878.png
报错:整数值不精确:第 1 行的列 'id' 的 'test'
二、Retrieve 查询
2.1、语法
SELECT
select_expr [, select_expr] ...
, ... ]
row_count | row_count OFFSET offset}] https://i-blog.csdnimg.cn/direct/509fca170a4c45d896b1971b412c7904.png
2.2、示例
先创建如下空表:
https://i-blog.csdnimg.cn/direct/3eb0d534237e4eb68dd3bd3d111f5224.png
插入数据后,查询 student 表中的全部列:
https://i-blog.csdnimg.cn/direct/684e97cf99fa4714a4f8e245591d160a.png
2.3、扩展
2.3.1、指定列查询
https://i-blog.csdnimg.cn/direct/54ebc08393a848fb87f0183d509977f3.png
2.3.2、查询字段为表达式
https://i-blog.csdnimg.cn/direct/31763738411a45948fe02117cc1a315d.png 2.3.3、列+常量
当我们给这些同学记载好结果后,发现语文某题堕落了,我们需要补偿给每位同学10分,这时我们可以用到该方法:
https://i-blog.csdnimg.cn/direct/e762ab7829f54aec9b424b566479676c.png
2.3.4、列+列
既然列可以在需要的环境下加一个常量,那么列可以加列吗?答案是可以的!我们来演示一下,求一下每为同学的总结果:
https://i-blog.csdnimg.cn/direct/19bec05acc9f48108529a8e159b734ac.png
2.3.5、为查询结果指定别名
上图所示可能有些贫苦,我们可以使用 as 简化一下,即可代替列名 如下:
select id,name,Chinese,Math,English,Chinese+Math+English as '总分' from student; https://i-blog.csdnimg.cn/direct/a23661fd7f1b406b9b81ea28592612c7.png
注意:
(1)as 是可省略的
(2)别名可以不加单引号,但是别名中若存在空格,必须加引号
(3)生成存在别名列的表为临时表,执行完输出之后就被删除了
(4)每个字段名后都可以加 as 代替当前字段名
2.4、去重查询
https://i-blog.csdnimg.cn/direct/0e2857379dd84097abf596ebfe51f058.png
不久,我们班级新来了一个同学叫做“光头强”,顺遂通过了考试,我们发现其英语结果竟然和“懒羊羊”结果一样!
https://i-blog.csdnimg.cn/direct/e0d34b8be8304bccb1ee0978dbdb5df6.png
英语老师要求列出全部英语分数,但是禁绝有重复的,我们就可以使用 distinct 关键字去重 :
https://i-blog.csdnimg.cn/direct/3b4b02d3656e40ac9e072bc84f428846.png
2.5、Where 条件查询
2.5.1、运算符
https://i-blog.csdnimg.cn/direct/299bca572f434129a6c0d99df66ec217.png
https://i-blog.csdnimg.cn/direct/8df1be10d4c9469b8e1d18922c8ab00f.png2.5.2、示例
2.5.2.1、基本查询
(1)查询英语为35分的同学
注意:此时当某位同学的英语结果为NULL,查询结果会忽略该同学
select id,name,English
from student
where English=35; https://i-blog.csdnimg.cn/direct/9bb4ae26617a4ad7bbdec6ae1e7d0eba.png
(2) 查询英语结果大于语文结果的同学
select id,name,Chinese,English
from student
where English>Chinese; https://i-blog.csdnimg.cn/direct/665b4d2bfb0b4b7eba70b46346ce9a58.png
2.5.2.2、AND和OR
(1)查询语文结果大于70分或英语结果大于70分的同学
select id,name,Chinese,English
from student
where Chinese>70 or English>70; https://i-blog.csdnimg.cn/direct/7d626487c1bb48969d3d310e3a0dc45a.png
2.5.2.3、范围查询
(1)查询数学结果范围在70和90之间的同学
select id,name,Math
from student
where Math between 70 and 90; https://i-blog.csdnimg.cn/direct/bb5541d018434100b4c2fdfbb7702586.png
2.5.2.4、含糊查询
select id,name
from student
where name like '%羊羊'; https://i-blog.csdnimg.cn/direct/5ab0209e87ef4a1c849b9c51b3555e63.png 多组其他测试如下:https://i-blog.csdnimg.cn/direct/3511447a2de540ab801e8453777cc1b9.png
2.5.2.5、NULL的查询
同学们学习了几天后,又来了一位新同学“熊二”,由于它在英语考试中途太饿了,想要吃蜂蜜,以是英语结果暂时没有:https://i-blog.csdnimg.cn/direct/1ab94360d41844a38a96fafe68d719a9.png
但是老师不记得是谁没考英语结果,就想调查一下:
select id,name,English
from student
where English is null; https://i-blog.csdnimg.cn/direct/6d1e6193928d46158f85262f011ef39c.png 2.6、Order by 排序
2.6.1、语法
SELECT ... FROM table_name ORDER BY {col_name | expr } [ASC |
DESC], ... ; 升序:ASC 降序:DESC
2.6.2、总结果排名
select id,name,Chinese+Math+English as total
from student
order by total desc; https://i-blog.csdnimg.cn/direct/34f26faad5114a309c222cc9b3bd58cf.png
(1)某列为NULL参与运算,与其他数值比较时,NULL被视为比任何值都小(包含负数)
(2)ORDER BY 子句中可以使用列的别名进行排序
(3)查询中没有ORDER BY 子句,返回的顺序是未定义的
2.7、分页查询
老师要查找总结果前三的同学及其总结果:
select id,name,Chinese+Math+English as total
from student
order by total desc
limit 0,3; https://i-blog.csdnimg.cn/direct/3c3dbb92f39d4dcca24994a208d60eea.png
三、总结
本篇我们详细讲解了增添,查询的各个操作,如有缺漏,欢迎在评论区增补,看完本篇,我们来增补一个小知识,观察下面代码:
https://i-blog.csdnimg.cn/direct/25185ece2cea49eb9c87f9606ec807bd.png
https://i-blog.csdnimg.cn/direct/4ccf7b23af1d42bcb65f73edc8a9228c.png 为什么使用 where 条件查询时,不能使用别名?
https://i-blog.csdnimg.cn/direct/833bc267ee014feda408f02dc78cda83.png
奥奇小课堂:
这和MYSQL中执行SQL语句的顺序有关:
(1)要查询数据,先要确定表,先执行 from
(2)查询过程中要根据查询条件把符合条件的数据过滤出,此时执行 where
(3)执行 select 后面的指定列,这些列需要加入最终结果
(4)排序操作,根据 order by 指定的列名和排序规则执行最后的排序
本篇到此结束,内容详细,制作不易,求赞啦啦啦~~
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]