创建数据库example01,并且创建表student,并且往表中插入数据:
- create database example01;
- use example01;
可以使用show databases查看本机中共有哪些数据库。
- create table students(
- id int,
- student_name varchar(20),
- sex char(1),
- age int,
- achievement double(5,2),
- team_id int,
- graduation_date date
- );
使用desc students查看表结构:
- insert into students values(1,'XiaoMing',0,7,100,1,'2020-7-1');
- insert into students values(2,'XiaoHong',1,8,199,1,'2020-7-1');
- insert into students values(3,'XiaoLiang',0,15,60,2,'2020-7-1');
- insert into students values(4,'XiaoMing',0,18,88,2,'2020-7-1');
- insert into students values(5,'XiaoZhang',1,2,110.5,3,'2020-7-1');
- insert into students values(6,'XiaoLi',0,6,30,3,'2020-7-1');
- insert into students values(7,'XiaoLiu',0,3,null,4,'2020-7-1');
- insert into students values(8,'zhangsan',0,6,50,4,'2020-7-1');
- insert into students values(9,'WangWu',0,6,40,4,'2020-7-1');
- insert into students values(10,'XiaoEr',0,6,40,null,'2020-7-1');
使用select * from students,查询students表中所有数据:
select * from students;
select student_name,achievement from students;
select student_name,achievement,age+18 from students;
该查询不会影响数据库中数据。
select student_name,achievement,age+18 as 真实年龄 from students;
select * from students where id=1;
select * from students where student_name='xiaoming' and age=18;
select * from students where age>10;
select * from students where age>=3 and age <=15;
select * from students where age between 3 and 15;
select * from students where achievement is null;
select * from students where age=3 or age =15 or age=18;
select * from students where student_name like '%Xiao%';
select * from students where achievement>=60 order by achievement asc;
如果是升序的话,【asc】可以省略不写。
select * from students where achievement>=60 order by age asc;
select * from students where achievement>=60 order by achievement desc;
select * from students order by age asc,achievement asc;