DQL数据查询语言
关键字:select
格式:
select ....from 表名 where 条件 group by 分组字段 having 条件 order by 排序字段 ase | desc
例子:
创建表
create table students(
sid varchar(20) parmary key auto_increment,
sname varchar(20),
sage varchar(20),
sclass varchar(20),
sgrade varchar(20),
sdate timestamp
);
操作表:
insert into students values (null,'张三','18','初三2班','58',null);
insert into students values (null,'李四','16','初三1班','62',null);
insert into students values (null,'王麻子','21','初三2班','73',null);
insert into students values (null,'赵五','19','初三3班','34',null);
insert into students values (null,'孙七','20','初三4班','66',null);
insert into students values (null,'奥特曼','20','初三5班','74',null);
练习:
1、查询所有学生
select * from students;
2、查询学生姓名和成绩
--格式:
select 字段名1,字段名2 from 表名;
select sname ,sgrade from students;
3、查询所有学生的年龄
--去重操作 distinct
--格式: select distinct 字段名,字段名2 from 表名;
select sage from students;
select distinct sage from students;
4、将所有学生的成绩+10分进行显示(别名)
--可以在查询的结果之上进行运算,不影响数据库中的值
--给列起别名 格式 :字段名 [as] 别名
select sgrade+10 from students;
select sgrade+10 [as] 学生成绩 from students;
select sgrade+10 [as] '学生成绩' from students;
错误的: select sgrade+10 [as] 学 生 成 绩 from students; --别名不能有多余空格,除非加引号
备注:由于个人原因,本博客暂停更新。如有问题可联系本人,本人提供技术指导、学习方向、学习路线。本人微信wlp1156107728(添加注明来意) QQ1156107728(添加注明来意)