DQL语句(数据查询语言)
创建数据库表(create table 表名(内容))
create table teacher(id int,name VARCHAR(20),salary FLOAT(4,2));
插入数据(insert into 表名 value(内容))
INSERT INTO teacher
VALUE
(1, '老边', 23.23);
过滤表中重复的语文成绩
select distinct chinese from student;
查找(select 查找的字段 from 表名 where 条件)
1.在所以学生的英语分数上加上十分特长分
select * from student;
使用as给字段起别名
select name,english+10 as english from student;
as 可以省略
select name,english+10 english from student;
2.计算学生的所有分数
select name,chinese+math+english as '总分' from student;
select name,(chinese+math+english)/3 as '平均分' from student;
3.查询姓名为王五的学生成绩
使用where条件过滤
select * from student where name='ww' and id=3;
4.查询数学成绩大于90的同学
select * from student where math>=90;
5.查询总分大于250的同学
select * from student where (chinese+english+math)>250;
运算符练习(><= ,and or not ,between...and... ,in ,like '_a%')
6.查询英语成绩在70到75之间的同学
select * from student where english between 70 and 75;
select * from student where english >=70 and english <=75;
7.查询语文成绩为80,81,82的同学
select * from student WHERE chinese in (80,81,82);
8.查询姓l的同学
select * from student where name like 'l%';
按照特定匹配模式查找
查询第二个字母为s的同学
select * from student where name like '_s%';
查询数学和语文成绩都大于80的同学
select * from student where math>80 and chinese >80;
升序降序(desc降序 asc升序)
(1)对数学成绩排序后输出。
不写排序规则默认升序排列,asc是升序
select * from student order by math;
降序
select * from student order by math desc;
指定多个字段进行排序,先按照第一个字段进行排序,如果相同则按照第二个字段排序
select * from student order by math desc,english desc;
(2) 对总分排序后输出,然后再按从高到低的顺序输出
select name,chinese+math+english as total from student order by total desc;
限制显示的行数(limit 关键字来限制)
(3)对姓l的学生成绩排序输出
select * from student where name like 'l%' order by chinese;
显示student 表格中的前3行。
select * from student limit 3;
显示 student 表格中的第3~5行。第一个参数代表偏移几行,第二个参数代表显示几个数据
select * from student limit 2,3;