MySQL入门篇——(四)查询数据之单表查询(模糊查找)()

一.单表查询
语法:
select *|字段列表|表达式 from table_name [where 条件] [order by 字段列表]
.说明:
1*:相当于按照表中字段顺序罗列表中的所有字段

select * from student

2.字段列表:当查询结果只是表中部分字段时,可将要显示的字段罗列出来,字段之间以逗号间隔

select name,address from student

3.表达式:可以是算术运算符也可以是数据库中的函数

select age+1 from student;
select length(name) from student;

4.table_name:表名
5.where:指定查询过滤条件
6.order by:对查询结果进行排序

下面详述关于where关键字在SQL语句中的情况

#创建一个学生表单
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','吉林省长春市宽平区');
#添加完成后运行查询语句,就会得表单中的数据情况
select * from student

在这里插入图片描述

# 符号 *在开发中不建议使用,运行效率低,尽可能明确的指出需要查询的内容的索引
select id,name,age,mobile,address from  stadent
#生成别名
select address as addr from student
#
select age,age+1 from student

在这里插入图片描述
like模糊查询

#like模糊查询
#不使用通配符,查询会输出所有name为张三的行(可以向表中添加一条id不同,name为张三的数据行验证)
select * from student where name like'张三'
#匹配0次或多次,输出所有以‘张’开头的name的行(个人认为此处的匹配次数可以理解为含有循环的个数?)
select * from student where name like'张%'
#只匹配1次,输出所有两个字的,第一个字是‘张’的name的行
select * from student where name like'张_'

结果分别如下所示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
如果我要查找名字中第二个字是#的人,我like‘ * * *’是不会成功的,*在sql语句中是通配符,like‘ * * *’只会输出所有人,如何查找,就使用到了escape关键字

#escape取消%或_字符的通配符特性
#查询姓名中含有%字符的学生信息
select * from student where name like '%#%%' escape#’
#查询姓名中含有%字符的学生信息
select * from student where name like '%$_%' escape ‘$’
#查询姓名以%%结尾的学生信息
select * from student where name like '%#%#%' escape '#’
#逻辑运算符 and or not
select * from student where age = 10 or age =11

#关系运算符 
select * from student where age > 10 

#between and			#输出大小在之间的数剧的行
select * from student where age between 10 and 24 #包括两端
select * from student where age between 24 and 10 #小值在前,大值在后,不允许颠倒,否则查不出结果

#观察我们知道表格中有一行数据的的地址如果想输出这行,使用如下代码可以吗?
select * from student where address=null 
#通过运行我们会发现输出的是一条空行

#这时候需要使用到 is
select * from student where address is null
select * from student where address is not null

select * from student where age between 10 and 24展示如下
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44724446/article/details/90055741
今日推荐