mysql> create table students (
-> id int unsigned primary key auto_increment,
-> sn int unsigned unique key,
-> name varchar(20) not null,
-> telephone varchar(32) unique key
-> );
一次插入一行数据:
mysql复制代码
mysql> insert into students(sn, name, telephone) values (23402101, '张三', '2024001');
mysql> insert into students values (10, 23402110, '李四', '2024010'); # 全列插入
values左侧忽略时就表明要进行全列插入,每一列都要指定数据
1.2 多行数据 + 指定列插入
mysql复制代码
mysql> insert into students values (11, 23402111, '王五', '2024011'), (12, 23402112, '赵六', '2024012');
values右侧用逗号隔开,表明插入多行数据
1.3 插入更新
mysql复制代码
mysql> insert into students values (12, 23402122, '田七', '2024022'); #主键冲突
ERROR 1062 (23000): Duplicate entry '12' for key 'PRIMARY'
mysql> insert into students values (13, 23402113, '田七', '2024012'); # 唯一键冲突
ERROR 1062 (23000): Duplicate entry '2024012' for key 'telephone'
当插入时出现冲突的时候,可采用同步更新操作,语法:
mysql复制代码
insert into ... on duplicate key update column = value [, column = value] ...
mysql> replace into students (sn, name, telephone) values (234021015, '老八', 2024015);
Query OK, 1 row affected (0.01 sec)
mysql> replace into students (sn, name, telephone) values (234021015, '小八', 2024015);
Query OK, 2 rows affected (0.00 sec)
2. retrieve
mysql复制代码
mysql> create table exam_ret(
-> id int unsigned primary key auto_increment,
-> name varchar(20) not null,
-> chinese float default 0.0 comment '语文成绩',
-> math float default 0.0 comment '数学成绩',
-> english float default 0.0 comment '英语成绩'
-> );
mysql> insert into exam_ret (name, chinese, math, english) values
-> ('张三', 88, 84, 77),
-> ('李四', 75, 91, 66),
-> ('王五', 45, 82, 73),
-> ('赵六', 94, 42, 51),
-> ('田七', 62, 67, 98);
2.1 select列
2.11 全列查询
mysql复制代码
mysql> select * from exam_ret;
*表示通配,在实际并不推荐采用*进行全列查询
数据库服务一般在服务器上;
线上服务器数据库里面存储的数据量非常大
2.12 指定列查询
mysql复制代码
mysql> select name, math from exam_ret;
2.13 查询字段为表达式
mysql复制代码
mysql> select name, math, chinese+math+english from exam_ret;
也可也将表达式名称命名
mysql复制代码
mysql> select name, math, chinese+math+english as total from exam_ret;
mysql复制代码
mysql> select name 姓名, math 数学, chinese+math+english 总分 from exam_ret;
2.14 结果去重
mysql复制代码
mysql> select distinct chinese from exam_ret;
2.2 where条件
比较运算符:
运算符
说明
>, >=, <, <=
大于,大于等于,小于,小于等于
=
等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL
<=>
等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1)
!=, <>
不等于
BETWEEN a0 AND a1
范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1)
IN (option, ...)
如果是 option 中的任意一个,返回 TRUE(1)
IS NULL
是 NULL
IS NOT NULL
不是 NULL
LIKE
模糊匹配。% 表示任意多个(包括 0 个)任意字符;_ 表示任意一个字符
逻辑运算符:
运算符
说明
AND
多个条件必须都为 TRUE(1),结果才是 TRUE(1)
OR
任意一个条件为 TRUE(1), 结果为 TRUE(1)
NOT
条件为 TRUE(1),结果为 FALSE(0)
2.21 各种成绩查询
++查找成绩不及格的同学:++
mysql复制代码
mysql> select name, chinese from exam_ret where chinese<60;
mysql> select name, math from exam_ret where math<60;
mysql> select name, english from exam_ret where english<60;
++查找语文成绩在[80,100]的同学:++
mysql复制代码
mysql> select name, chinese from exam_ret where chinese>=80 and chinese<=100;
mysql> select name, chinese from exam_ret where chinese between 80 and 100;
++查找指定英语成绩:++
mysql复制代码
mysql> select name, english from exam_ret where english=98 or english=97 or english=51;
mysql> select name, english from exam_ret where english in (98, 97, 51);
++查找姓张和张某:++
mysql复制代码
mysql> select name from exam_ret where name like '张%'; #姓孙
mysql> select name from exam_ret where name like '张_'; #孙某
++查找英文成绩好于英语成绩的同学:++
mysql复制代码
mysql> select name, chinese, english from exam_ret where chinese > english;
++查找总分小于225的同学:++
mysql复制代码
mysql> select name, chinese + math + english total from exam_ret where total<225;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
在上面的sql语句中,执行顺序是:
先执行from,要知道在哪个表当中筛选数据;
再执行where,要知道筛选的条件,拿着条件去筛选;
最后拿着条件去指定表里面的指定列筛选
所以这里报错不认识这个total别名
mysql复制代码
mysql> select name, total from exam_ret where chinese + math + english total<225;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'total<200' at line 1
上面这样也不行,因为对列做重命名,已经是最后一步了,将数据拿完,最后改一下名字
mysql复制代码
mysql> select name, chinese+math+english total from exam_ret where chinese+math+english<225;
++查找语文成绩大于80且不姓张的同学:++
mysql复制代码
mysql> select name,chinese from exam_ret where chinese>80 and name not like'张%';
++查找张某同学,其他的同学成绩必须>225且英语>80:++
mysql复制代码
mysql> select name, chinese, math, english, chinese+math+english total from exam_ret where name like '张_' or (chinese+math+english>225 and english>80);
2.22 null的查询
mysql复制代码
mysql> select * from t11 where name is null;
mysql> select * from t11 where name is not null;
null表示没有,' '表示有,但是是空串
2.3 结果排序
语法:
mysql复制代码
select ... from table_name [where ...] order by column [asc|desc], [...]
#asc 为升序(默认为asc)
#desc 为降序
如果有null,null视为比任何值都小
++语文成绩升序排序:++
mysql复制代码
mysql> select name,chinese from exam_ret order by chinese;
++语文成绩升序、数学成绩降序、英语成绩降序:++
这个意思是,如果语文成绩一样,按数学降序排序;如果语文、数学成绩一样,按英语降序排序
mysql复制代码
mysql> select name, chinese, math, english from exam_ret order by chinese asc, math desc, english desc;
++查询总分,降序排序++
mysql复制代码
mysql> select name, chinese+math+english as total from exam_ret order by total desc;
为什么where后面无法使用别名,而这里可以使用:
在排序之前,是要先有合适的数据
先执行from,知道在哪个表
如果有where子句,再执行where,拿到筛选条件
筛出要的数据
筛选出的数据进行排序
order by在name, chinese+math+english as total语句后面,所以order by后面可以跟别名
++查询姓张或者姓王的同学数学成绩,数学成绩降序:++
mysql复制代码
mysql> select name, math from exam_ret where name like '张%' or name like '王%' order by math desc;
2.4 筛选分页结果
mysql复制代码
mysql> select * from exam_ret limit 5; #从表的开始连续读取5行
mysql> select * from exam_ret limit 3,5; #从指定位置(3, 下标从0开始)开始,连续读取5行