首先创建一个table并插入数据
create table student(
id char(36) primary key,
name varchar(8) not null,
age int(3) default 0,
mobile char(11),
address varchar(150)
)
insert into student
values ('9b4435ec-372c-456a-b287-e3c5aa23dff4','张三',24,'12345678901','北京海淀');
insert into student
values ('a273ea66-0a42-48d2-a17b-388a2feea244','李%四',10,'98765432130',null);
insert into student
values ('eb0a220a-60ae-47b6-9e6d-a901da9fe355','张李三',11,'18338945560','安徽六安');
insert into student
values ('6ab71673-9502-44ba-8db0-7f625f17a67d','王_五',28,'98765432130','北京朝阳区');
insert into student
values ('0055d61c-eb51-4696-b2da-506e81c3f566','王_五%%',11,'13856901237','吉林省长春市宽平区');
执行完毕后可以通过执行下任何一句代码读取到之前的table
select * from student//*相当于按照表中字段顺序罗列表中的所有字段
select id,name,age,mobile,address from student//当查询结果只是表中部分字段时,可将要显示的字段罗列出来,字段之间以逗号间隔
select后可以是算术运算符也可以是数据库中的函数
select age+1 from student;
select length(name) from student;
select所选字段可以指定别名以使结果更便于理解,字段名与别名之间使用空格或as关键字间隔,例如:
select address adr from student
where:指定查询过滤条件
like:进行数据模糊查询
%表示该位置上可以有0个或者n个字符
_表示该位置上只有一个字符
select * from student where name like '张_';#查询两个字的张姓学生信息
select * from student where name like '张%';#查询姓张的学生信息
select * from student where name like '%李%';#查询姓名中含有“李”字的学生信息
思考下边代码,是否可以实现查询姓名中含有%字符的学生信息
select * from student where name like '%%%';
执行后发现并不可以,此时需要使用escape来取消%或_字符的通配符特性
select * from student where name like '%#%%' escape '#';
下边说明and, or,not(暂未使用到),between and方法
select * from student where name like '张%' and age>20
select * from student where age=10 or age=11//与下面代码等价
select * from student where age in (10,11)
#between and 包括两端,顺序必须从小到大,否则无法查到数据
select * from student where age between 10 and 24;
select * from student where address is null
select * from student where address is not null
此处应该注意到 is null不能写成 = null,同样,is not null不能写成!=null,否则查询不到数据
select * from student where address is null
select * from student where address is not null
数据库中几个常用的函数
select name,length(name) from student #获取存储长度,一个汉字字符占三个字节
select count(id) from student#计数
select max(age) from student #找最大值,最小值min()
select avg(age) from student#求平均数
到此为止,我们先暂时关闭这个表格
drop table student
为了方便之后的讨论,我们再创建一个新的table
create table student(
id char(1) primary key,
name varchar(8),
sex char(2) default '男' ,
age int(3) default 0
)
insert into student values ('1','王明','男',18);
insert into student values ('2','孙丽','女',17);
insert into student values ('3','王明','男',27);
insert into student (id,sex,age) values ('4','男',27);
了解几个简单的概念
单行函数:仅对单条数据中的列进行操作并且返回一个结果
多行函数:可以操作成组的多条数据,每组返回一个结果,所以多行函数又称之为组函数;
单行函数——字符串函数
select length(name) from student#一个汉字三个字节,返回字符串存储长度
select char_length(name) from student#获取字符串中字符个数
select concat(id,',',name,',',sex) from student#拼接
select concat_ws(',',id,name,sex,age) from student#按照规则拼接
select reverse(name) from student#反转
select trim(name) from student#去掉字符串左右两端的空格
select replace(name,'#','!') from student#字符串中的替换
#substr(str,pos[,len]):从源字符串str中的指定位置pos开始取一个字串并返回,从第一位开始;len是截取的长度
select substr('hello world',7,5) str from dual;//dual是MySQL数据库中默认的一个表
单行函数——数值函数
select mod(1,3)#取余
select round(1.58,0),round(1.58),round(1.298,1);#四舍五入的保留,后的参数表示小数点后保留几位,默认为0
select truncate(1.58,0),truncate(1.298,1);#字符串的截取 ,后的参数代表.后有几位
单行函数——日期函数
select now();//获取当前日期
select date_format(now(),'%Y-%m-%d-%H:%i:%s');//获取指定格式日期
select datediff(now(),'2019-05-01')//获取当前日期和参数日期的差值
select timediff(now(),'2019-05-10 11:15:00')//获取当前时间与参数时间的差,此处注意,两个参数类型必须相同
单行函数——转换函数
convert(value,type):将value转换为type类型,type可以是char(字符型)、date(日期型)、time(时间型)、datetime(日期时间型)、 signed(整型) 和decimal(浮点型)类型中的一个
select '666'+666,convert('111',signed)+111#类型转换
单行函数——其它函数
//可以参考三目运算符理解
select if(name is null,'未知',name) from student;
select if(name is not null,name,'未知')from student
select ifnull(name,'未知') from student//相当于对之前的简化
未完待续…