ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

数据库-查询-知识点合集

2020-11-17 22:02:35  阅读:740  来源: 互联网

标签:知识点 -- where 数据库 sscore 查询 student 合集 select


-- 单表查询

select from where group by having
order by limit

-- 子查询 定义:
-- 在一个 select 语句中嵌套一个 select 语句 ,也可以在
-- 后续讲解的 update delete insert中嵌套一个select语句,
-- 那么这个嵌套的select语句就是子查询语句。

-- 1、查询年龄最大的学生信息
select * from t_student where sage =
(
select max(sage) from t_student
)

-- 2、查询平均分在500以上的 系id, 系名称
-- 当子查询的结果有多个时,需要使用in
select did ,dname from t_dept where did in
(
select did from t_student GROUP BY
did having avg(sscore) > 480
)

-- 3、查询与魏德心相同年龄的学生
select * FROM t_student where sage =
(
select sage from t_student where sname ='魏德心'
)
and sname !='魏德心'

-- 找出比全校平均年龄小的每个班的平均年龄
select sclass , avg(sage) from t_student
group by sclass having avg(sage)<
(
select avg(sage) from t_student
)

-- 多行子查询 (int 、 not in 、 all , any)
-- 查询年龄与Tea或Ice或Eat年龄相同的学生的姓名。

select * from t_student where sage in (
select sage from t_student where sename in (
'Tea','Ice','Eat'
)
)

<>
-- 查询不是3班的学生
select * from t_student where sclass <>3;

-- any :满足子查询的任意一个结果
-- 找出比考试分数500以上任何一个学生年龄大的学生的姓名
(any :大于其中任意一个)
select * from t_student where sage >any
(
select sage from t_student where sscore >500
)
-- 等价于 大于子查询结果的最小值
select * from t_student where sage >
(
select min(sage) from t_student where sscore > 500
)

-- all : 满足子查询的所有结果

-- 找出比考试分数500以上每个学生年龄都大的学生的姓名。
select * from t_student where sage >=all(
select sage from t_student where sscore > 500
)

-- 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;

-- 找出年龄小于18且高考分数大于500且1班的学生。

-- 找出年龄大于20或者3班的学生。

-- 找出90年以后出生或者1班的学生或者学号1207以后的学生,结果按照成绩降序排列。

-- 找出年龄在17和20之间且高考分数500以下的学生。

-- 找出apple、orange、egg的信息

-- 模糊查询 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 '%\' -- 第一个\表示转义 第二个\表示被转义的字符(看成普通字符)

-- 查询比任何班的平均分都大的学生信息
select * from t_student where sscore >all(
select avg(sscore) from t_student GROUP BY sclass
)

-- 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}$';

-- 查询中文

-- 查询数字的另一个写法

'[[:digit:]]'
例: 查询英文名中含有空格的学生:
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 '^[[:upper:]]'
		 -- 匹配空格
		 select * from t_student where sename regexp '[[:blank:]]'
		 
		 -- 匹配字母
		  select * from t_student where sename regexp '[[:alpha:]]'
			
			-- 匹配中文 个数
		   select * from t_student where sname regexp '^[\\u4e00-\\u9fff]{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 from t_student;

-- instr(源字符,目标字符) 与 locate(目标字符,源字符) 用法一样
select instr('hellomysql','my');

-- lpad(源字符,总长度,填充的字符) rpad
select lpad('hello',10,'abcdef');
select rpad('hello',10,'*')

-- 2、 日期和时间函数
-- 用于处理mysql的日期和时间的函数
-- 获取当前日期的函数
select curdate();
select current_date();

-- 获取当前时间函数
select curtime();
select current_time();

-- 获取当前日期和时间的函数
select now();
select sysdate();
select CURRENT_TIMESTAMP;
select LOCALTIME();

-- 获取国际时间 和 国际日期
select UTC_DATE(),UTC_TIME();

-- 获取日期的月份 年份
select month(sysdate()),YEAR(sysdate()),day(sysdate()), MONTHNAME(sysdate())

-- 获取星期的函数
dayname :星期名
dayofweek() 一个星期的第几天, 周天是第一天
dayofmonth() :一个月的第几天
dayofyear(): 一年的第几天
select dayname(now()),DAYOFWEEK(now()),DAYOFMONTH(now()),DAYOFYEAR(now());

-- 获取星期函数名
week() : 一年的第几周 从0开始
weekofyear: 一年的第几周 从1开始
select WEEK(now()),WEEKOFYEAR(now());

-- 获取季度 quarter
select quarter(now());

-- 获取日期的指定函数
extract(unit from date)
select extract( year from now());
select extract( year from sbir) ,sbir from t_student;
select * from t_student;

-- 将当前时间转成毫秒数
select TIME_TO_SEC(CURRENT_TIME());

-- 日期相加减
adddate(d,n)
addtime(d,s) -- 加的秒
select ADDDATE(now(),3) , addtime(now(),100);

--subdate(d,n) subtime()
addda
-- 增加指定的单位
select adddate(now() ,INTERVAL 3 month)

-- 计算两个时间的差值
datediff(date1,date2) -- 返回天数
select DATEDIFF(now(),'2020-12-1');
select timeDIFF(CURRENT_TIME(),'12:12:12');

-- 格式化日期
DATE_FORMAT(date,format)
select DATE_FORMAT(now(),'%Y/%m/%d')

select DATE_FORMAT(now(),'%Y %a %b %h:%m:%s')

select DATE_FORMAT(now(),'%T');

-- 3 数学函数
select RAND();

-- 绝对值
select abs(-123);
-- 格式化数字 保留3位小数
select FORMAT(1234567.8899,3)

select ceil(12.5),ceil(-12.5) ,floor(12.7),floor(-12.7)

-- 取模
select mod(10,3);

-- truncate 霸道截取 保留1位小数
select TRUNCATE(12.35,1)

-- MySql的聚合函数

-- 将多条记录筛选过滤后返回一行记录(聚合)
-- max() : 取出这一列最大值
-- min(): 取出这一列最小值
-- count(): 返回这一列的 总行数
-- sum() : 返回 这里列的总和
-- avg():返回平均值

-- 查询成绩最高分
select max(sscore) from t_student;
-- 查询最小年龄
select min(sage) from t_student;

select min(sename) from t_student;

-- 查询二班的总成绩
select sum(sscore) ,max(sscore) from t_student where Sclass =2;

-- 查询女生总人数
select count(*) from t_student where ssex ='女';
-- 以sscore 为基准
select count(sscore) from t_student where ssex ='女';

-- 求平均值
select avg(sscore) from t_student where sclass = 1;

-- 如果求每个班的平均值 ? 需要分组
group by

-- 格式化保留2位小数
select sclass, format(avg(sscore),2) from t_student group by sclass
-- 保留2位小数
select sclass, ROUND(avg(sscore),2) from t_student group by sclass

-- 查询女生男生的平均年龄 和 最大年龄 最小年龄
select avg(sage) , max(sage) , min(sage) from t_student group by ssex

-- 查询每个班的男生 ,女生的最高分和最低分
-- 多字段分组使用 , 隔开
select sclass ,ssex, max(sage),min(sage) from t_student group by sclass ,ssex
order by sclass ,ssex ;

-- 在分组的基础上进行筛选 需要使用 having
-- 在分组之前筛选 就使用 where
-- 查询每个班 的平均分大于500的班级
select sclass , avg(sscore) from t_student group by sclass having avg(sscore)>500 ;

select * from t_student

-- 统计400分以上学生的每个班的平均分 ,求平均分在500以上的班
select sclass , sname , avg(sscore) avgscore from t_student where sscore >400 GROUP BY Sclass
having avgscore> 500

-- 问题1: 在mysql中 查询的结果 是否只能是 分组字段 和 聚合函数字段呢?
-- 如果增加非聚合字段,则MySql 中也不错误,默认显示分组的第一条记录信息,而Oracle语法错误

--  问题2 : where 和having的区别
--  where 是普通结果的筛选条件  在分组之前执行  
--  having  : 必须有分组才可以使用 , 先分组再筛选

-- limit 用法: 在查询结果上截取指定记录数
-- 查询前3行记录
select * from t_student order by sscore desc limit 3;
-- 分页查询   查询第一页  查询第二页   假设一页显示5行
select * from t_student order by sscore desc limit 0,5;  //参数1 表示下标 参数2 查几条 
-- 查询第二页  currentPage  pageSize =5
select * from  t_student order by sscore desc limit 5 , 5 ;

	select * from  t_student order by sscore desc limit  (currentPage -1 )*pageSize ,pageSize 

rollup  按分组字段汇总再平均
select  Sclass ,avg(sscore) from t_student group by sclass 
with ROLLUP

select  Sclass ,ssex,avg(sscore) from t_student group by sclass ,ssex
with ROLLUP

-- 在学生信息表中将18岁以上且分数是440以上的学生按照系编号分组,统计每个组的分数的平均值,再输出平均分大于500分的组系编号和平均分

select did ,avg(sscore) from t_student where sage>=18 and  sscore>440 group by did 
having avg(sscore)>500


-- 在学生信息表中将系编号、班级编号不为空的学生按照班级和年龄分组,统计每个组的平均分数、最高分、最低分、人数(分别取别名),输出每个组的编号和统计数据,去掉人数小于3的组。

select sclass ,sage ,avg(sscore),max(sscore),min(sscore),count() from t_student where did is not null and sclass is not null
group by sclass , sage having count(
)>=3

-- 在学生信息表中将系编号和班级编号不为空的学生按照班级分组,统计每个组有多少个不同的年龄(即去掉年龄的重复)并取别名“不同年龄”,输出每个组的班级编号和不同年龄的个数,并按照年龄个数升序排列,取前两个结果。

select sclass ,count(distinct sage) as 不同年龄个数 from t_student where did is not null and sclass is not null
group by sclass
order by 不同年龄个数 asc limit 2

select * from t_student order by sclass

-- 在学生信息表中将1班和3班的学生且年龄是18岁以上的学生按照系编号分组,如果系编号为空则改为0。统计每个组的人数个数、最大年龄、最高分数(分别取别名),输出结果中人数大于1的组的信息,并按照系编号降序排列取第一个结果。

select did , count() ,max(Sage) ,max(sscore) from t_student where sclass in (1,2) and sage >18 group by ifnull(did,0)
having count(
)>1 order by did desc limit 1


  1. 0-9 ↩︎

标签:知识点,--,where,数据库,sscore,查询,student,合集,select
来源: https://www.cnblogs.com/z5452830/p/13996796.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有