MySQL中的查询过滤条件where |
您所在的位置:网站首页 › where作用于数据行 › MySQL中的查询过滤条件where |
查询
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 输出结果为:%也可以指代字符数0 输出结果为 怎么样查出含有%的信息?如下语句可行吗? 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 '___'; #三个下划线输入结果为 1,and select * from student where name like '张%' and age>10;输出结果为 输出年龄为10或者11的学生信息 注意:此处的(10,11)不为集合,只代表两个值 验证:如下查询语句 select * from student where age in (10,24)集合中含有年龄为17的,上述查询语句的结果为 查询结果如下 #小数在前,大数在后,且包括大数和小数 select * from student where age between 10 and 24输出结果为 1,select所选字段后面可以指定别名以使查出来的结果所显示的字段更好理解,字段名与别名之间使用空格或as关键字间隔;为了阅读方便,推荐使用as关键字这种方式定义字段别名。 增加别名语句:以address为例,adddress [as] addr, as可以省略 select address as addr from student输入结果为 如果不加双引号,上述两个SQL语句无法执行 补充: select * from student #此语句查出所有的信息,*为通配符,此法书写只是为了方便,一般不使用*号,用下边语句代替 select id,name,age,mobile,address from student 表达式在查询过程中可能用到表达式,例如年龄为int类型的,输出age+1.语句如下 select age,age+1 from student;输出结果为 输出结果中字段名改为new_age,如下 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |