MySQL中的查询过滤条件where

您所在的位置:网站首页 where作用于数据行 MySQL中的查询过滤条件where

MySQL中的查询过滤条件where

2024-07-09 17:53| 来源: 网络整理| 查看: 265

查询 wherelike(模糊查询)%下划线(_) and,or,notbetween and 别名表达式

where

首先创建一个表并添加如下数据,在下述查询中均用到此表

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','吉林省长春市宽平区'); like(模糊查询) %

%可以指代字符个数为0到n。 如下查询语句

select * from student where name like '张%';

输出结果为:%可以指代字符数1到n 在这里插入图片描述 再添加如下数据,然后查询

insert into student value('0055d61c-eb51-4696-b2da-506e81c3f577','张',17,'13856901237','吉林省长春市宽平区') select * from student where name like '张%';

输出结果为:%也可以指代字符数0 在这里插入图片描述

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 '#'; 1,%#%%中#%是整体,去掉第二个%的特殊含义,第一个和第三个不变,之后#%中%变为普通的字符,且#可换为字母和一些特殊字符,如#、$、,、\等字符。 2,escape后面单引号中只能是单个字符;

查询结果如下,查出含有%号的数据 在这里插入图片描述

下划线(_)

每个下划线仅指代一个字符 如下查询语句

select * from student where name like '张_';

输出结果如下,查不出表中name为 张 和 张李三的数据 在这里插入图片描述 下划线(_)且仅指代一个字符

如下查询语句

select * from student where name like '___'; #三个下划线

输入结果为 在这里插入图片描述

and,or,not

1,and

select * from student where name like '张%' and age>10;

输出结果为 在这里插入图片描述 2,or

select * from student where age=10 or age=11;

输出年龄为10或者11的学生信息 在这里插入图片描述 上述语句等价于

select * from student where age in (10,11)

注意:此处的(10,11)不为集合,只代表两个值 验证:如下查询语句

select * from student where age in (10,24)

集合中含有年龄为17的,上述查询语句的结果为 在这里插入图片描述 3,如果值为空,用如下语句查询

select * from student where address is null

查询结果如下 在这里插入图片描述 值不为空,用如下语句查询

select * from student where address is not null between and

#小数在前,大数在后,且包括大数和小数

select * from student where age between 10 and 24

输出结果为 在这里插入图片描述

select * from student where age between 24 and 10 #没有结果 别名

1,select所选字段后面可以指定别名以使查出来的结果所显示的字段更好理解,字段名与别名之间使用空格或as关键字间隔;为了阅读方便,推荐使用as关键字这种方式定义字段别名。 增加别名语句:以address为例,adddress [as] addr, as可以省略

select address as addr from student

输入结果为 在这里插入图片描述 2,在默认情况下,查询结果中显示的字段名为大写字母,如果别名中包含空格、特殊字符(例如%)或者对大小写敏感,则别名需要使用双引号引起来。

select age+1 "stu age" from student; select length(name) as "name%length" from student;

如果不加双引号,上述两个SQL语句无法执行

补充: select * from student #此语句查出所有的信息,*为通配符,此法书写只是为了方便,一般不使用*号,用下边语句代替 select id,name,age,mobile,address from student 表达式

在查询过程中可能用到表达式,例如年龄为int类型的,输出age+1.语句如下

select age,age+1 from student;

输出结果为 在这里插入图片描述 给age+1取别名有

select age,age+1 as new_age from student;

输出结果中字段名改为new_age,如下 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3