一、基础查询筛选
1、 排序查询
order by 字段名 desc/asc
查询所有学生信息 按分数 降序 ctrl+shift+r
select * from t_student order by sscore desc;
按照年龄升序 (其中asc可以省略,默认升序)
select * from t_student order by sage asc ;
组合排序 order by 字段1 asc/desc ,字段2 asc/desc
按照班级升序 和 分数降序排列
select * from t_student order by sclass , sscore desc ;
2 数据过滤 where 条件
查询1班的所有学生
select * from t_student where sclass =1;
where 条件后面的常用操作符号
= 、!= 、 > 、 >= 、 < 、<= 、<> 、 not 、 is null,is not null
select * from t_student where sclass!=2;
查询分数在500以上的学生
select * from t_student where Sscore >=500;
查询没有成年的学生(未满18)
select * from t_student where sage <18
查询在1990年到1999年之间出生的学生信息 两个并列的 使用 and 排序
select * from t_student where sbir>=‘1990-01-01’ and sbir<=‘1999-12-31’ order by sbir asc;
查询分数为空 的学生信息
select * from t_student where sscore is null;
查询分数不为空的学生信息
select * from t_student where sscore is not null
3、 where 的多条件查询
and or not in between …and
查询500分以上的女生信息
select * from t_student where sscore >= 500 and ssex =‘女’;
查询 查询男生 或者20以上的女生 and 和 or 优先级: and > or
select * from t_student where ssex=‘男’ or sage>=20 and ssex=‘女’;
查询 20以上的男生和18以上的女生
select * from t_student where ssex=‘男’ and sage>=20 or ssex=‘女’ and sage>=18 order by ssex desc
查询年龄时 18 20 22 的学生 多个值使用in筛选 ,符合列表的条件全部查询
select * from t_student where sage in (18,20,22);
查询 不是18 22 (效率不高)
select * from t_student where sage not in(18,22)
查询分数在500-600 之间的学生 包括两个端点
select * from t_student where sscore between 500 and 600 ;
等价
select * from t_student where sscore >=500 and sscore <=600;
模糊查询 like
通配符 % : 匹配任意个数的任意字符
_: 匹配任意一个字符
查询姓张的学生
select * from t_student where sname like ‘张%’;
查询英文名字中 有 s/S 包含关系
select * from t_student where sename like ‘%s%’ or sename like ‘%S%’;
查询英文名第二个字母是 o 的学生信息
select * from t_student where sename like ‘_o%’;
如果查询的字符 是特殊字符 需要转义
update t_student set sename =‘P%ad’ where sid=1228;
查询英文名中包含 % 的 转义 #表示转义符 ,转义其后面的一个符号
select * from t_student where sename like ‘%#%%’ escape ‘#’;
\ 不需要使用 escape
select * from t_student where sename like ‘%%%’
查询 英文字符中 包含 \的信息
select * from t_student where sename like ‘%\’ 第一个\表示转义 第二个\表示被转义的字符(看成普通字符)
二、正则表达式
MySql 正则表达式 expression regular
MySql正则是 子串匹配,存在包含关系
查询英文字母中包含a
select * from t_student where sename REGEXP ‘[a]’;
查询英文字母中包含a b c 其中一个字符
select * from t_student where sename regexp ‘[abc]’;
包含数字
select * from t_student where sename regexp ‘[0-9]’;
\d
select * from t_student where sename regexp ‘\d’;
查询手机号码 3个连续0
select * from t_student where sphone regexp ‘888’;
至少包含3个0 ,不一定连续
select * from t_student where sphone regexp ‘0.*0.*0’
或
select * from t_student where sphone like ‘%0%0%0%’
匹配次数
- :1次或多少
?: 0次或1次
- : 0次或多次
{n} : n次
{n,} :至少n次
{n,m} :n到m次
匹配手机号码的正确格式 由于只能有11位
select * from t_student where sphone regexp ‘^1[0-9]\d{9}$’;
查询英文字母中 包含 . 需要转义字符 \
select * from t_student where Sename regexp ‘\.’;
查询学号中第四位为’3’的学生学号与姓名。
select * from t_student where sid regexp ‘1{3}3’
select * from t_student where sid like ‘___3%’;
查询年龄以8结尾的学生姓名与年龄。
select * from t_student where sage regexp ‘8$’;
400多分
select * from t_student where sscore regexp ‘^4[0-9]{2}’;
SELECT * FROM T_STUDENT WHERE SPHONE REGEXP ‘^1[0-9]\d{9}$’;
查询中文
查询数字的另一个写法
例: 查询英文名中含有空格的学生:
select * from t_student where sename regexp ‘[[:blank:]]’;
查询英文名中含有大写字符的学生:
select * from t_student where sename regexp ‘[[:upper:]]’;
匹配英文名中有数字的另一个写法 [[:digit:]]
匹配包含数字
select * from t_student where sename regexp ‘[[:digit:]]’;
匹配首字母大写
select * from t_student where sename regexp ‘2’
匹配空格
select * from t_student where sename regexp ‘[[:blank:]]’
匹配字母
select * from t_student where sename regexp ‘[[:alpha:]]’
匹配中文 个数
select * from t_student where sname regexp ‘3{3}$’;
\d 匹配数字 \w 匹配数字字母 —— $ \s 匹配空白字符
\D 匹配非数字 \W 匹配非数字 字母 —— $ \S 匹配非空白
三、文本和日期函数
MySql的函数
MySql的函数分为 单行函数和聚合函数
内置的很多单行函数为了解决查询的先关问题
单行函数包括
1、字符串函数
left 返回左边指定长度字符串
select left(‘abcdefg’,3);
select left(sename,2),sename from t_student;
right 返回右边指定长度
select right(‘abdefg’,4);
字符串长度
length()
select length(‘hello’);
返回英文字母长度 一个中文= 3个长度
select length(sename) as 英文长度 ,sename ,
length(sname) as 中文长度,sname
from t_student;
locate(str , s) 返回str在s中第一次出现的位置
select locate(‘e’ ,‘hello’); 从1 开始计数
locate (str,s
,pos)从pos位置开始查找,返回str在s中的位置
select locate(‘ee’ ,sename,3),sename from t_student;
lower 转小写
select lower(sename) from t_student ;
upper 转大写
select upper(sename) from t_student ;
reverse 反转
select reverse(‘abcd’);
ltrim :去掉左边的空格 rtrim去掉右边的空格
select length(ltrim(’ abc ‘));
select length(rtrim(ltrim(’ abc ')));
substring(str,pos) : 截取 pos起始到结尾
select substring(‘helloworld’,6); 从1开始
substring(str,pos,len): 截取pos起始和指定长度
select substring(‘helloworld’,6,3);
substr
select substr(‘helloworld’,6);
select substr(‘helloworld’,6,3);
字符个数
select CHAR_LENGTH(sname) ,sname from t_student;
select CHAR_LENGTH(sename) ,sename fr