中国建设银行手机银行网站,资质做网站需要用到什么,国内前10电商代运营公司,创意设计英文翻译文章目录 1. 注释2. 新增(Create)3. 查询(Retrieve)3.1 全列查询3.2 指定列查询3.3 查询字段为表达式3.4 别名3.5 去重: distinct3.6 排序: order by3.7条件查询3.8 分页查询 4. 修改 (update)5. 删除(delete)6. 内容重点总结 1. 注释
注释#xff1a;在SQL中可以使用“–空格… 文章目录 1. 注释2. 新增(Create)3. 查询(Retrieve)3.1 全列查询3.2 指定列查询3.3 查询字段为表达式3.4 别名3.5 去重: distinct3.6 排序: order by3.7条件查询3.8 分页查询 4. 修改 (update)5. 删除(delete)6. 内容重点总结 1. 注释
注释在SQL中可以使用“–空格描述”来表示注释说明 CRUD 即增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写。
2. 新增(Create)
语法insert into 表名 values (字段1, 字段2, ...), (字段1, 字段2, ...), ...其中字段1, 字段2, …对应了创建表时对应的位置. 如果要使插入的数据与相应的列相对应, 则可以insert into 表名 (列名1, 列名2, ...) values (字段1, 字段2, ...), (字段1, 字段2, ...), ...如果插入的数据量比设计表时数据量少就会给少的位置赋予null值. 具体操作举例如下: 我们先创建一张Student表, 具体设定如下 示例1 直接多行全列插入数据insert into student values(1,张三),(2, 李四); 示例2 多行指定列插入insert into student(name) values(王五), (赵六);
3. 查询(Retrieve)
语法
select[distinct] {* | {column [, column] ...} --字段[from table_name]--表名[where...]--查询条件[order by column [asc| desc], ...]--排序limit...--分表查询着非常复杂, 下面我们来一步一步去拆分讲解.
---我们先创建一个考试成绩表, 并插入适当的数据, 方便我们接下来的分析
create table exam(id int, name varchar(20), chinese decimal(3, 1), math decimal(3, 1), english decimal(3, 1));insert into exam values
(1,唐三藏, 67, 98, 56),
(2,孙悟空, 87.5, 78, 77),
(3,猪悟能, 88, 98.5, 90),
(4,曹孟德, 82, 84, 67),
(5,刘玄德, 55.5, 85, 45),
(6,孙权, 70, 73, 78.5),
(7,宋公明, 75, 65, 30);3.1 全列查询
-- 通常情况下不建议使用 * 进行全列查询
-- 1. 查询的列越多意味着需要传输的数据量越大
-- 2. 可能会影响到索引的使用。
mysql select * from exam;3.2 指定列查询
-- 指定列的顺序不需要按定义表的顺序来
select id, name, english from exam;3.3 查询字段为表达式
-- 表达式不包含字段
select id, name, 10 from exam;
-- 表达式包含一个字段
select id, name, english10 from exam;
-- 表达式包含多个字段
select id, name, chinesemathenglish from exam;3.4 别名
为查询结果中的列指定别名表示返回的结果集中以别名作为该列的名称语法:
SELECT column [AS] alias_name [...] FROM table_name;-- 结果集中表头的列名别名
select id, name, chinesemathenglish as 总分 from exam;3.5 去重: distinct
--通过查询得知98分重复了
select math from exam;
------
| math |
------
| 98.0 |
| 78.0 |
| 98.0 |
| 84.0 |
| 85.0 |
| 73.0 |
| 65.0 |
--------去重查询
select distinct math from exam;
------
| math |
------
| 98.0 |
| 78.0 |
| 84.0 |
| 85.0 |
| 73.0 |
| 65.0 |
------3.6 排序: order by
语法
-- ASC 为升序从小到大
-- DESC 为降序从大到小
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...] ORDER BY column [ASC|DESC], [...];1.没有 order by 子句的查询返回的顺序是未定义的永远不要依赖这个顺序 2.null数据排序视为比任何值都小升序出现在最上面降序出现在最下面
--不加desc, 默认升序排序
select id, name, chinesemathenglish as 总分 from exam order by 总分;
-------------------------
| id | name | 总分 |
-------------------------
| 7 | 宋公明 | 170.0 |
| 5 | 刘玄德 | 185.5 |
| 1 | 唐三藏 | 221.0 |
| 6 | 孙权 | 221.5 |
| 4 | 曹孟德 | 233.0 |
| 2 | 孙悟空 | 242.5 |
| 3 | 猪悟能 | 276.0 |
-------------------------
--加desc, 降序排序
select id, name, chinesemathenglish as 总分 from exam order by 总分 desc;
-------------------------
| id | name | 总分 |
-------------------------
| 3 | 猪悟能 | 276.0 |
| 2 | 孙悟空 | 242.5 |
| 4 | 曹孟德 | 233.0 |
| 6 | 孙权 | 221.5 |
| 1 | 唐三藏 | 221.0 |
| 5 | 刘玄德 | 185.5 |
| 7 | 宋公明 | 170.0 |
---------------------------多列排序, 分先后
select id, name, chinese, math from exam order by chinese desc, math;
--------------------------------
| id | name | chinese | math |
--------------------------------
| 3 | 猪悟能 | 88.0 | 98.0 |
| 2 | 孙悟空 | 87.5 | 78.0 |
| 4 | 曹孟德 | 82.0 | 84.0 |
| 7 | 宋公明 | 75.0 | 65.0 |
| 6 | 孙权 | 70.0 | 73.0 |
| 1 | 唐三藏 | 67.0 | 98.0 |
| 5 | 刘玄德 | 55.5 | 85.0 |
--------------------------------
--先对Chinese进行降序排序, 在不影响原来排序结果的基础上再对math进行升序排序3.7条件查询
比较运算符 逻辑运算符 注意: 1.WHERE条件可以使用表达式但不能使用别名。 2.AND的优先级高于OR在同时使用时需要使用小括号()包裹优先执行的部分 示例:
基本查询
-- 查询英语不及格的同学及英语成绩 ( 60 )
select name, english from exam where english 60;
--------------------
| name | english |
--------------------
| 唐三藏 | 56.0 |
| 刘玄德 | 45.0 |
| 宋公明 | 30.0 |
--------------------
-- 查询语文成绩好于英语成绩的同学
select name, chinese, english from exam where chinese english;
-----------------------------
| name | chinese | english |
-----------------------------
| 唐三藏 | 67.0 | 56.0 |
| 孙悟空 | 87.5 | 77.0 |
| 曹孟德 | 82.0 | 67.0 |
| 刘玄德 | 55.5 | 45.0 |
| 宋公明 | 75.0 | 30.0 |
-----------------------------
-- 查询总分在 200 分以下的同学
select name, chinese math english as 总分 from exam where chinese math english 200;
-------------------
| name | 总分 |
-------------------
| 刘玄德 | 185.5 |
| 宋公明 | 170.0 |
-------------------and 与 or
-- 查询语文成绩大于80分且英语成绩大于80分的同学
select * from exam where chinese 80 and math 80;
-----------------------------------------
| id | name | chinese | math | english |
-----------------------------------------
| 3 | 猪悟能 | 88.0 | 98.0 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
-----------------------------------------
-- 查询语文成绩大于80分或英语成绩大于80分的同学
select * from exam where chinese 80 or math 80;
-----------------------------------------
| id | name | chinese | math | english |
-----------------------------------------
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孙悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 猪悟能 | 88.0 | 98.0 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 刘玄德 | 55.5 | 85.0 | 45.0 |
-----------------------------------------
--and的优先级比or高范围查询
--1. between...and...-- 查询语文成绩在 [80, 90] 分的同学及语文成绩
select name, chinese from exam where chinese between 80 and 90;
-- 使用 AND 也可以实现
select name, chinese from exam where chinese 80 and chinese 90;
--------------------
| name | chinese |
--------------------
| 孙悟空 | 87.5 |
| 猪悟能 | 88.0 |
| 曹孟德 | 82.0 |
----------------------2. in-- 查询数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
select name, math from exam where math in (58, 59, 98, 99);
-- 使用 OR 也可以实现
select name, math from exam where math 58 or math 59 or math 98 or math 99;
-----------------
| name | math |
-----------------
| 唐三藏 | 98.0 |
| 猪悟能 | 98.0 |
-----------------模糊查询: like
-- % 匹配任意多个包括 0 个字符
select name from exam where name like 孙%;
-----------
| name |
-----------
| 孙悟空 |
| 孙权 |
-----------
-- _ 匹配严格的一个任意字符
select name from exam where name like 孙_;
--------
| name |
--------
| 孙权 |
--------NULL的查询: is [not] null
-- 查询 qq_mail 已知的同学姓名
select name, qq_mail from student where qq_mail is not null;-- 查询 qq_mail 未知的同学姓名
select name, qq_mail from student where qq_mail is null;3.8 分页查询
语法
-- 起始下标为 0
-- 从 0 开始筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- 从 s 开始筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
-- 从 s 开始筛选 n 条结果比第二种用法更明确建议使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;分页示例
-- 第 1 页
select * from exam order by id limit 3 offset 0;
-----------------------------------------
| id | name | chinese | math | english |
-----------------------------------------
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孙悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 猪悟能 | 88.0 | 98.0 | 90.0 |
-----------------------------------------
-- 第 2 页
select * from exam order by id limit 3 offset 3;
-----------------------------------------
| id | name | chinese | math | english |
-----------------------------------------
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 刘玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孙权 | 70.0 | 73.0 | 78.5 |
-----------------------------------------
-- 第 3 页如果结果不足 3 个不会有影响
select * from exam order by id limit 3 offset 6;
-----------------------------------------
| id | name | chinese | math | english |
-----------------------------------------
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
-----------------------------------------4. 修改 (update)
语法
UPDATE table_name SET column expr [, column expr ...][WHERE ...] [ORDER BY ...] [LIMIT ...]案例
-- 将孙悟空同学的数学成绩变更为 80 分
update exam set math 80 where name 孙悟空;-- 将曹孟德同学的数学成绩变更为 60 分语文成绩变更为 70 分
update exam set math 60, chinese 70 where name 曹孟德;-- 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
update exam set math math 30 order by chinese math english limit 3;-- 将所有同学的语文成绩更新为原来的 2 倍
update exam set chinese chinese * 2;5. 删除(delete)
语法
DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]案例
-- 删除孙悟空同学的考试成绩
delete from exam where name 孙悟空;-- 删除整表数据
delete from exam;6. 内容重点总结
新增
-- 单行插入
insert into 表(字段1, ..., 字段N) values (value1, ..., value N);
-- 多行插入
insert into 表(字段1, ..., 字段N) values
(value1, ...),
(value2, ...),
(value3, ...);查询
-- 全列查询
select * from 表
-- 指定列查询
select 字段1,字段2... from 表
-- 查询表达式字段
select 字段1100,字段2字段3 from 表
-- 别名
select 字段1 别名1, 字段2 别名2 from 表
-- 去重DISTINCT
select distinct 字段 from 表
-- 排序ORDER BY
select * from 表 order by 排序字段
-- 条件查询WHERE
-- (1)比较运算符 (2)BETWEEN ... AND ... (3)IN (4)IS NULL (5)LIKE (6)AND (7)OR
(8)NOT
select * from 表 where 条件修改
update 表 set 字段1value1, 字段2value2... where 条件删除
delete from 表 where 条件