网站如何挂马教程,icp备案在哪里查询,网页制作 收集资料,wordpress 站内资讯目录
新增#xff08;create#xff09;
插入单条记录
插入多条记录
查询#xff08;retrieve#xff09;
查询所有列
查询特定列
查询字段为表达式
别名
去重
排序
按单列排序
按多列排序
使用表达式或别名排序
排序NULL值
条件查询
比较运算符
逻辑运算…目录
新增create
插入单条记录
插入多条记录
查询retrieve
查询所有列
查询特定列
查询字段为表达式
别名
去重
排序
按单列排序
按多列排序
使用表达式或别名排序
排序NULL值
条件查询
比较运算符
逻辑运算符
基本查询
AND和OR
范围查询
BETWEEN AND
IN
模糊查询
NULL 的查询
分页查询
修改update
删除delete? 在本篇文章中我们来学习 Ccreate 增加Rretrieve 查询Uupdate 更新Ddelete 删除即 数据的增删改查
新增create
要想插入数据我们首先得有数据表因此我们先创建一张学生表 USE test; DROP TABLE IF EXISTS student; – 若学生表已经存在则删除 CREATE TABLE student( id INT, name VARCHAR(20), age INT, class VARCHAR(10) ); 学生表已经创建好了
接下来我们就可以往表中插入数据了
插入单条记录
语法 INSERT INTO table_name (column1, column2, column3, …) VALUES (value1, value2, value3, …); 全列插入所有数据都插入时可以省略 (column1, column2, column3, …) insert into student values(1, “张三”,18, “1班”); 插入成功 其中 Query OK表示执行成功 1 row affected表示操作影响了一行记录 (0.01 sec)表示执行时间 ERROR表示操作失败出现了错误
Column count doesn’t match value count at row 1描述了错误原因列数量与第一行中的值数量不匹配
而若只插入指定列就需要使用 (column1, column2, column3, …)column 的数量必须和 value 的数量一致 insert into student (id, name, age) values (2, “李四”, 19); 插入多条记录
语法 INSERT INTO table_name (column1, column2, column3, …) VALUES (value1_1, value2_1, value3_1, …), (value1_2, value2_2, value3_2, …), …; 插入两条数据 insert into student values (3, “王五”, 18, “2班”), (4, “赵六”, 19, “1班”); 指定列插入两条数据 insert into student (id, name, age)values (5, “一一”, 18), (6, “二二”, 19); 注意 1插入的数据类型要与表中列的数据类型匹配 2插入数据时列的顺序必须和 VALUES 中的顺序一致 3若表的列中有 NOT NULL 约束必须提供这些列的值除非有默认值 查询retrieve
查询所有列
语法 select * from table_name; 我们查询刚才插入的所有数据 由于 student 表中的数据不多因此很快就将所有的数据都查询出来了但是当表中的数据很多时此时查询所有数据就需要花费很多时间
由于 mysql 是 客户端服务器 结构的 当查询的数据量很大时select * from table_name 操作就会产生大量的硬盘 IO 和网络 IO就可能把硬盘和网卡的带宽吃满服务器就无法正常响应了而若此时其他的客户端向服务器发送请求服务器就无法响应数据其他客户端就会认为服务器挂了
因此通常情况下并不建议使用 * 进行全列查询查询的列越多意味着需要传输的数据量越大
查询特定列
语法 SELECT column1 column2, … FROM table_name; 注指定列的顺序不需要安装定义表的顺序来
示例
我们查询 id 和 年龄 select id, age from student; 查询字段为表达式
语法 select expr, … from table_name; 示例
表达式中不包含字段 select id, name, 20from student; 表达式中包含一个字段 select id, name, age 20 from student; 在查询出结果后会将每一行带入表达式进行运算
当前的表达式查询并没有修改服务器上硬盘存储的数据本体只是在查询结果的基础上进行运算得到的是一个 临时表当这个查询操作结束时这里的数据age 20也就没有了数据库服务器硬盘内容不会有任何改变
为了方便进行后续演示我们再创建一个 考试成绩表 drop table if exists exam_result; create table exam_result( id int, name varchar(10), chinese decimal(4,1), math decimal(4,1), english decimal(4,1) ); – 插入数据 insert into exam_result values (1, “一一”, 80.1, 70.9, 90.0), (2, “二二”, 60.9, 76.9, 90.5), (3, “三三”, 70.3, 96.3, 83.4), (4, “四四”, 83.7, 84.6, 75.3); 表达式中包含多个字段 select id, name, chinese math english from exam_result; 别名
上述 chinese math english 计算的是成绩总和我们一般会选择使用 总分 进行表示因此我们就可以以 总分 作为计算结果的临时别名
别名alias用于为表或字段指定临时名称以简化查询和提高可读性别名在查询的执行结果过程中不会改变数据库中的实际表名或列名
语法 SELECT column [AS] alias_name FROM table_name; 例如 select id, chinese math english as ‘总分’ from exam_result; as 可以省略即 select id, chinese math english’总分’ from exam_result; 去重
当某列中有重复记录时我们就可以使用 DISTINCT对其进行去重
例如 select age from student; 去重 select distinct age from student; 排序
在 MySQL 中可以使用 ORDER BY 来对查询结果进行排序当没有使用 order by 子句时进行查询返回的结果是未定义无序的
按单列排序
语法 SELECT column1, column2 FROM table_name ORDER BY column_name [ASC|DESC]; 其中ASC 表示升序排序时默认是升序因此可以省略DESC 表示降序
例如
对数学成绩进行降序排列 select id, name, math from exam_result order by math desc; 对语文成绩进行升序排列 select id, name, chinese from exam_result order by chinese; 按多列排序
语法 SELECT column1, column2 FROM table_name ORDER BY column1 [ASC|DESC], column2 [ASC|DESC]; 按照多个列进行排序时先按照第一个列排序若第一个列出现值相同的情况则按照第二个列排序以此类推
例如
按照学生的年龄进行降序排列若年龄相同则按照 id 进行升序排列 select id, name, age from student order by age desc, id; 使用表达式或别名排序
例如
查询学生考试总分按照升序进行排列 select id, name, chinese math english as totalfrom exam_result order by total; 排序NULL值
null 数据参与排序时视为比任何值都小升序时出现在最上面降序时出现在最下面 条件查询
当我们只需要查询特定的数据时就可以使用 WHERE 进行条件查询过滤数据只返回满足特定条件的数据
在学习条件查询的语法之前我们先来学习一些运算符
比较运算符
运算符
说明
, , ,
大于大于等于小于小于等于 等于NULL不安全出现 NULL NULL 时结果为 NULL 等于NULL安全出现 NULL NULL 时结果为 true
!,
不等于
BETWEND a AND b
范围匹配[a, b]若 a value b返回 true
IN (option, …)
若是 option 中的任意一个返回 true
IS NULL
是 NULL
IS NOT NULL
不是 NULL
LIKE
模糊匹配% 表示任意多个字符_ 表示任意一个字符
逻辑运算符
运算符
说明
AND
多个条件必须都为 true结果才为 true
OR
任意一个条件为 true结果都为 true
NOT
条件为 true 时结果为 false
注意 1WHERE 条件中可以使用表达式但是不能使用别名 2AND 的优先级高于 OR在同时使用且需要先执行 or 时需要使用 () 小括号包裹优先执行的部分 接下来我们来学习如何进行条件查询
查询语法 SELECT column1, column2 FROM table_name WHERE condition; 基本查询
例如
查询年龄为 18 的学生 select id, name, age from student where age 18; 查询总分大于240的学生 select name, chinese math english as ‘总分’ from exam_result wherechinese math english 240; 查询英语成绩大于语文成绩的学生 select name, chinese, english from exam_result where english chinese; AND和OR
查询 数学成绩高于80分 并且 语文成绩也高于80分 的同学 select name, math, chinese from exam_result where math 80 and chinese 80; 查询 数学成绩高于80分 或语文成绩也高于80分 的同学 select name, math, chinese from exam_result where math 80 or chinese 80; 观察 AND 和 OR 的优先级 select name, chinese, math, english from exam_result where chinese 80 or math 80 and english 80; 上述查询相当于 mysql select name, chinese, math, english from exam_result where chinese 80 or (math 80 and english 80); 若要先使用 OR则需要加上 () select name, chinese, math, english from exam_result where (chinese 80 or math 80) and english 80; 范围查询
BETWEEN AND
查询 数学成绩在 80- 90 的同学 select name, math from exam_result where math between 80 and 90; IN
查询 id 为 1、3、4、9 的学生 select id, name from exam_result where id in (1, 3, 4, 9); 模糊查询
使用 LIKE 进行模糊查询
我们往 student 表中插入数据 insert into student (id, name, age) values (7, “张一一”, 20), (8, “张二二”, 17); 使用 _ 匹配任意一个字符 select name from student where name like ‘张_’; 使用 % 匹配任意多个包括 0 个字符 select name from student where name like ‘张%’; NULL 的查询
通过 IS [NOT] NULL 查询 NULL 值 或 过滤 NULL 值
查询班级已知的同学 select name, class from student where class is not null; 查询班级未知的同学 select name, class from student where class is null; 分页查询
语法 SELECT … FROM table_name [WHERE…] [ORDER BY … ] LIMIT count; SELECT … FROM table_name [WHERE…] [ORDER BY … ] LIMIT offset, count; SELECT … FROM table_name [WHERE…] [ORDER BY … ] LIMIT countOFFSET offset; offset开始的行号行号从 0 开始
count返回的行数
当不指定 offset 时默认从 0也就是第一条记录开始若指定 offset 则从offset 开始显示 n 条数据
例如
按照 id 进行分页获取第一页3条数据 select id, name from student limit 3; 按照 id 进行分页获取第二页3条数据 select id, name from student limit 3, 3; 按照 id 进行分页获取第三页3条数据 select id, name from student limit 3 offset6; 修改update
语法 UPDATE table_name SET column1 value1 [, column2 value2] [WHERE condition] [ORDER BY condition] [LIMIT]; 例如
将 id 为 3 的学生的数学成绩修改为 87 update exam_result set math 87 where id 3; 将所有学生的语文成绩加上 5 分 update exam_result set chinese chinese 5; 将总成绩倒数的同学英语成绩加上 10 分 update exam_result set english english 10 order by chinese math english limit 1; 删除delete
语法 DELETE FROM table_name [ WHERE …] [ ORDER BY … ] [ LIMIT … ]; 例如
删除 id 为 3 的学生数据 delete from exam_result where id 3; 删除总分最高的两名学生数据 delete from exam_result order by chinese math english desc limit 2; 删除表中所有数据 delete from exam_result;