15621857753

mysql数据库子句:Having Orderby Limit Like

来源:齐鲁CMS 栏目:Mysql 阅读: 日期:2024-09-28

mysql数据库子句:Having Orderby Limit Like

30.Having 子句

Having 的本质和 where 一样,用来进行数据条件筛选

Having 可以在 group by 子句之后,针对分组数据进行统计筛选, where 不行

案例:

-- >select class_id,count(*) as number from my_student where count(*)>=4 group by class_id; //不能运行

-- >select class_id,count(*) as number from my_student group by class_id having count(*) >= 4; //可以运行

-- >select class_id,count(*) as number from my_student group by class_id having number >= 4; //可以运行,number为count(*)的别名

31.Order by 子句:根据校对规则对数据进行排序

基本语法:order by 字段 [asc|desc]; //默认升序 asc

案例:班级学生按照身高排序

-- >select from my_student order by stu_height asc;

改进上述案例:按照班级降序以及身高升序

-- >select from my_student order by class_id desc,stu_height; //身高升序默认可省略

32.Limit 子句:用来限制记录数量获取;可以用来分页

基本语法:Limit 数量;

案例1:查看前两名学生的信息

-- >select from my student limit 2;

案例2:分页获取数据,每页2个数据

-- >select from my_student limit 0,2; // 一二名

-- >select from my_student limit 2,2; // 三四名

33.运算符案例 between and in is

案例1:查找年龄区间(20-30岁),三个方法:

-- >select from my_student where stu_age between 20 and 30;

-- >select from my_student where stu_age between 30 and 20;

-- >select from my student where stu_age >20 and stu_age <30;

案例2:查找身高高于176的或者年龄大于20岁的学生

-- >select from my_student where stu_height >=170 or stu_age >=20;

案例3:查看指定stu_id的学生信息

-- >select from my_student where stu_id in ('stue001','stue004','stu0007');

案例4:查询为空的数据 (不为空则 is not null)

-- >select from my_int where int_6=null; // 不行,int_6为字段

-- >select from my_int where int_6 is null; // 可以,int_6为字段,

34.Like 运算符:用来模糊匹配

基本语法:like '匹配模式';

两种占位符:

_ 匹配单个字符

% 匹配多个字符

案例:获取所有姓小的同学

--select * from my_student where stu_name like'小_'; // 两个字的,比如小猪

--select * from my_student where stu_name like'小%'; // 多个字的,比如小猪,小猪猪