Linux——MySQL数据查询(day18)

目录

一、条件查询

1.1 比较运算符

1.2 逻辑运算符

1.3 模糊查询

1.4 in

1.5 between

1.6 null值判断

1.7 排序

二、聚合函数(统计函数)

2.1 count、max、min

2.2 数学函数

2.3 avg、round

2.4 时间函数

2.5 字符串函数

2.5.1 substr、left、right

2.5.2 length

2.5.3 upper、lower 

2.5.4 concat

三、分组与分页

3.1 分组

3.1.1 分组查询

3.1.2 as 取别名

3.1.3 分组后条件筛选(having)

3.2 limit分页

四、连接查询

4.1 基本连接查询

4.2 mysql三种连接查询

4.2.1 内连接查询

4.2.2  右连接查询

4.2.3 左连接查询

4.2.4 案例

4.3 子查询

4.3.1 标量子查询 

4.3.2 列级子查询

4.3.3 行级子查询

4.3.4 表级子查询

五、保存查询结果

六、小结

​6.1 sql子查询

6.2 mysql数据查询

七、练习


一、条件查询

1.1 比较运算符

 

1.2 逻辑运算符

1.3 模糊查询

1.4 in

1.5 between

1.6 null值判断

1.7 排序

 

 因为这里id不重复,所以直接按id降序了,hometown升序没用到

二、聚合函数(统计函数)

2.1 count、max、min

 count返回有13组数据

 

2.2 数学函数

2.3 avg、round

2.4 时间函数

2.5 字符串函数

2.5.1 substr、left、right

(1)substr

2.5.2 length

返回的是字节的个数,一个汉字为3个字节

 

2.5.3 upper、lower 

2.5.4 concat

三、分组与分页

3.1 分组

3.1.1 分组查询

 

 

3.1.2 as 取别名

 

 

3.1.3 分组后条件筛选(having)

3.2 limit分页

 

四、连接查询

4.1 基本连接查询

 

4.2 mysql三种连接查询

4.2.1 内连接查询

4.2.2  右连接查询

 

4.2.3 左连接查询

4.2.4 案例

4.3 子查询

4.3.1 标量子查询 

 相当于返回一个值的

4.3.2 列级子查询

 

 2班级年纪大于1班级任意一个同学就出来

4.3.3 行级子查询

最上面的错误是因为max()已经进行了分组

4.3.4 表级子查询

五、保存查询结果

(1)

 

(2)合并查询 

六、小结

6.1 sql子查询

sql语句中any、some、all的使用

都是用在子查询里面 用作比较运算

any 表示任意一个  有一个满足了结果就为true  和some是一样的效果
all 全部满足了 结果才为True

场景:
查询出在学生表当中  2班级的学生年龄 都大于1班级的所有同学、

select * from students where class_id=2 and age>all(select age from students where class_id=1)

select * from students where class_id=2 and age>(select max(age) from students where class_id=1)

查询出在学生表当中  2班级的学生年龄 任意一个大于1班级的所有同学、

select * from students where class_id=2 and age>any(select age from students where class_id=1)

select * from students where class_id=2 and age>(select min(age) from students where class_id=1)


表级查询

select t1.xs,t1.banji from (select b1.name as xs ,b2.name banji from students as b1 inner join class as b2 on b1.class_id=b2.id) as t1

6.2 mysql数据查询

条件查询:

比较条件 跟在 where后面  > < =  !=   <>
in 查询 指定一个数据容器
between  表示一个区间   1 and 10    还可以表示一个时间范围

null值的判断 如果是一个空值对象 is去判断

若是 空字符串的话 则可以使用 = 去判断

order by 字段1、字段2  排序【asc、desc】

聚合函数:
count函数  max函数 min函数 length函数 
数学函数:

avg函数  round函数 时间函数 

字符函数:
substr(对象,start,lenght)

left right

分组和分页
group by 字段

起别名 as 

分组条件的筛选 where having区别:都是用来限定条件的

where是跟在from之后

having区别 用在group by之后

limit分页  select * from student limit start【读取的位置】,count【读取的数量】

连接查询:
mysql有三种连接查询:
内连接 inner join  两种表共同的数据项
外连接:left join  参考左边的表为基准查询表,右边的表用null填充
right join 参考右边的表为基准查询表,左边的表用null填充

子查询:
1:标量子查询 一行一列 单个值
2:列级子查询  一行多列 多个值
3:行级子查询  多行一列、
4:表级子查询  多行多列 【用来做数据源了】

保存查询结果:

insert into 表 select 查询来充当数据源

union 去重的效果

union all 将多次查询的结果合并输出

七、练习

-- 查询所有80后学生的姓名、性别和出生日期(筛选)
-- select sname as 姓名, if(gender,'男','女') as 性别,birth as 出生日期 from tb_student where birth BETWEEN '1980-1-1' and '1989-12-31'; 
-- select sname as 姓名,  CASE gender when 1 then '男' else '女' END as 性别,birth as 出生日期 from tb_student where birth BETWEEN '1980-1-1' and '1989-12-31'; 
-- 查询名字由4个中文字符的学生学号和姓名(运算+函数)
-- SELECT sname as 姓名,stuid as 学号 from tb_student where LENGTH(sname)/3=4  LENGTH 返回的是字节长度  一个中文3个字节 
-- 查询名字中有”不“字或“嫣”字的学生的姓名(模糊)
-- SELECT sname as 姓名,stuid as 学号 from tb_student where sname LIKE '%不%' or sname LIKE '%嫣%'
-- 查询学生选课的所有日期(去重)
-- SELECT DISTINCT seldate as 选课日期 from tb_score   DISTINCT 可以实现去重

 

-- 查询男学生的姓名和生日按年龄从大到小排列(排序)
-- SELECT sname as 姓名,birth as 生日 from tb_student where gender=1 ORDER By birth desc;
-- 查询每个学生的学号和平均成绩(分组和聚合函数)
-- SELECT sid as 学号,avg(mark) as 平均成绩 from tb_score group by sid;

-- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算)
-- SELECT sname as 姓名 from tb_student where stuid in (SELECT sid as 学号 from tb_score GROUP BY sid HAVING count(sid)>2);
-- 查询选课学生的姓名和平均成绩(子查询和连接查询)
-- SELECT sname as 姓名, avgMark  as 平均分 from tb_student t1 INNER JOIN (SELECT sid ,avg(mark) as avgMark from tb_score GROUP BY sid) as t2 on t1.stuid=t2.sid;
-- 查询每个学生的姓名和选课数量(左外连接和子查询)
SELECT sname as 姓名, IFNULL(total,0) as 选课数量 from tb_student  t1 LEFT JOIN (SELECT sid,count(sid) as total from tb_score GROUP BY sid) t2 on t1.stuid=t2.sid;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清园暖歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值