目录
1.查询
1.1条件查询:where
where不能用别名
select name,chinese+math+english as total from exam_result
where chinese+math+english>200;
查询名字以孙开头的同学的总分
select name,chinese+math+english as total from exam_result where name like '孙%';
1.2逻辑运算符
查询语文成绩大于80分,且英语成绩大于80分的同学
select name,chinese,english from exam_result where chinese>80 and english>80;
1.3范围查询
查询语文成绩在[80,90]分的同学及语文成绩
select name,chinese from exam_result where chinese between 80 and 90;
select name,chinese from exam_result where chinese >=80 and chinese<=90;
查询数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
select name,math from exam_result where math in(58,59,98,99);
select name,math from exam_result where math=58 or math=59 or math=98 or math=99;
1.4NULL的查询:IS[NOT]NULL
1.NULL在MySQL中比任何数值都小
2.NULL与别的值进行比较,都会返回FALSE ,0
3.NULL就是NULL,不是0
4.NULL与任何值运算都会返回NULL
查询英语成绩已知的同学姓名
select name,english from exam_result where english is not null;
1.4分页查询
从0开始,筛选n条结果
SELECT…FROM table_name[WHERE…] [ORDER BY…] LIMIT num;
select * from exam_result limit 3;
从s开始,筛选n条结果
SELECT …FROM table_name[WHERE…] [ORDER BY…] LIMIT start,num;
select * from exam_result limit 3,3;
从s开始,筛选n条结果
select * from exam_result limit 1 offset 5;
2.修改
语法:
UPDATE table_name SET column=expr [,column=expr…] [WHERE] [ORDER BY…] [LIMIT…];
将孙悟空同学的数学成绩变更为80分
update exam set math = 80 where name ='孙悟空';
将曹孟德同学的数学成绩变更为 60 分,语文成绩变更为 70 分
update exam_result set math =60,chinese=70 where name='曹孟德';
将总成绩倒数前三的 3 位同学的数学成绩加上 10 分
update exam_result set math=math+10 order by (chinese+math+english) asc limit 3;
将所有同学的语文成绩更新为原来的2倍
update exam_result set chinese=chinese*2;
3.删除
语法:DELETE FROM table_name [WHERE…] [ORDER BY…] [LIMIT…];
删除孙悟空同学的考试成绩
delete from exam_result where name='孙悟空';