show databases;
use db_name;
create database [if not exists] db_name;
drop database [if exists] db_name;
select database();
show tables
drop table if exists stu_test;
create table stu_test(
id int,
sn int comment '学号',//comment 是注释的意思 可以不写
name varchar(20) comment '姓名',
qq_mail varchar(20) comment '邮箱'
);
drop table if exists table_name;
desc table_name;或者describe table_name;
insert into stu_test values(1,2017001,'chen','[email protected]');
insert into stu_test (id,sn) values(3,2017003);
insert into stu_test values(3,2017003);这样就不可以 必须要指定要插入的列名
insert into stu_test (id,sn) values
(3,2017003),
(4,2017004),
(5,2017005);
SELECT
[DISTINCT] {* | {column [, column] ...}
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
LIMIT ...
select * from stu_test;
不过通常不建议使用 * 进行全列查询
– 1. 查询到的列越多,意味着要传输的数据量越大
– 2. 可能会影响到索引的使用
select id,name from stu_test;
比如说想要将1显示成2001
select id+2000,name from stu_test;
-- 从table_name中查找column并且再返回的结果集中将alias_name作为该列的名称
SELECT column [AS] alias_name [...] FROM table_name;
– a. 没有注明的情况下都是默认升序ASC
– b. null数据排序的时候,视为比任何数据都笑,升序出现在最上面,降序出现在最下面
-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
--可以对多个字段进行排序,排序优先级随书写的顺序
-- 查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示
SELECT name, math, english, chinese FROM exam_result
ORDER BY math DESC,english,chinese;
注意: where条件可以使用表达式,但不能使用别名
-- 查询英语不及格的
select * from score where english<60;
--英语 和 数学都=100
select * from score where english=100 and math=100;
--英语 或 数学都=100
select * from score where english=100 or math=100;
and的优先级高于or,在同时使用的时候,注意要用小括号包裹住先执行的部分
-- 查询数学和英语都>70的同学中,语文>80的同学
select * from score where chinese > 80 or math > 70 and english > 70;
-- 查询语文>80 或 数学>70的同学中,英语>70的同学
select * from score where (chinese > 80 or math > 70) and english > 70;
--查询英语成绩在90-100之间的同学
select * from score where english between 90 and 100;
-- 或者AND也可以实现
select * from score where english>=90 and english<=100;
-- 查询数学成绩是68.78.88的同学
select * from score where math in (68,78,88);
-- 或者OR也可以实现
select * from score where math=68 or math=78 or math=88;
-- %匹配任意多个字符(包括0个)
select * from stu_test where name like 'c%';
-- _严格匹配一个任意的字符
select * from stu_test where name like 'c_'
-- 查询qq邮箱已知的同学
select * from stu_test where qq_mail is not null;
-- 查询qq邮箱未知的同学
select * from stu_test where qq_mail is null;
– 从0开始筛选n条结果
select ... from table_name [where ...][order by ...] limit n;
– 从s开始筛选n条结果(注意不包含第s条数据哦)
-- 第一种
select ... from table_name [where ...][order by ...] limit s,n;
-- 第二种(比较推荐)
select ... from table_name [where ...][order by ...] limit s offset n;
【例】:按id进行分页,每页3条记录,分别显示1,2,3页
– 第一页
select * from stu_test order by id limit 3 offset 0;
select * from stu_test order by id limit 0,3;
– 第二页
select * from stu_test order by id limit 3 offset 3;
select * from stu_test order by id limit 3,3;
– 第三页
select * from stu_test order by id limit 3 offset 6;
select * from stu_test order by id limit 6,3;
update table_name set column = expr [,column = expr...] [where...][order by...][limit...]
-- 将所有同学的英语成绩变为原来的两倍
update stu_test set english = english*2;
-- 将dai同学的数学成绩变为100分
update stu_test set math=100 where name='dai';
-- 将数学成绩倒数前三的3个同学数学成绩+10
update stu_test set math=math+10 order by math limit 3;
delete from table_name [where...][order by...][limit...]
-- 删除dai同学的考试成绩
delete from stu_test where name='dai';
-- 删除整张表的数据
delete from stu_test;