SQL进阶day12——高级条件语句

1筛选限定昵称成就值活跃日期的用户

我的代码:答案不对

select uid,u.nick_name,u.achievement
from exam_record er join practice_record pr
using(uid) 
join user_info u using(uid) 
where u.nick_name like "牛客%号" 
and u.achievement between 1200 and 2500
and date_format(er.start_time,'%Y%m')='202109' 
or date_format(pr.submit_time,'%Y%m')='202109'

#以『牛客』开头『号』结尾
# where u.nick_name like "牛客%号"
#成就值在1200~2500之间
# and u.achievement between 1200 and 2500
#最近一次活跃(答题或作答试卷)在2021年9月
# and date_format(er.start_time,'%Y%m')='202109' or date_format(pr.submit_time,'%Y%m')='202109'

改正代码:

select uid,u.nick_name,u.achievement
from exam_record er join practice_record pr
using(uid) 
join user_info u using(uid) 
where u.nick_name like '牛客%号' 
group by u.uid
having u.achievement between 1200 and 2500
and date_format(max(er.start_time),'%Y%m')='202109' 
or date_format(max(pr.submit_time),'%Y%m')='202109'

但是最终结果还是不对:

正确代码:下面这两个表要左连接

exam_record er left join practice_record pr 

select uid,u.nick_name,u.achievement
from exam_record er left join practice_record pr
using(uid) 
left join user_info u using(uid) 
where u.nick_name like '牛客%号' 
group by u.uid
having u.achievement between 1200 and 2500
and date_format(max(er.start_time),'%Y%m')='202109' 
or date_format(max(pr.submit_time),'%Y%m')='202109'

复盘:

(1)模糊查询标准语法
●    select 字段名
●    from 表名
●    where 字段名 like '通配符+字符'

例:查询国家名中以C开头ia结尾的国家

where name like 'C%ia'

(2)最近的活动时间,用max:

date_format(max(pr.submit_time),'%Y%m')='202109'

(3)两个数据表的uid等数据:     

left join:29条记录

如果是inner join:19条记录

(只两个表保留相同的)

复习:表连接

1)内连接:只两个表保留相同的

2)左连接:合并后左边的表所有行都保留,若左边的表有空值则删除(即删除右边没有匹配上的)

(4)这里where,group by,having的先后顺序很重要:

深入理解一下group by:

1)先看一下3个表连接后的总表: 

发现相同的uid,对应的也是相同的nickname,

这种情况下如果先按照uid分组然后再where就会报错!

同理,u.achievement也是要在分组前进行的:

如果分组前直接筛选出 where u.nick_name like '牛客%号' 

其实仔细分析题目也能看出:

请找到昵称以『牛客』开头『号』结尾、成就值在1200~2500之间,(group by 之前)

且最近一次活跃(答题或作答试卷)在2021年9月的用户信息。(按照用户进行group by )

2筛选昵称规则和试卷规则的作答记录

我的代码:不出意外就要出意外了

# # 找到昵称以"牛客"+纯数字+"号"或者纯数字组成的用户
# select uid,nick_name
# from user_info u
# where nick_name like '牛客%号' or nick_name like '[0-9]'

# # 对于字母c开头的试卷类别(如C,C++,c#等)的,已完成的
# select 	exam_id,*
# from examination_info ei
# where tag like 'c%' or tag like 'C%'
# and submit_time is not NULL

select uid,exam_id,
avg(score)avg_score
from exam_record
where submit_time is not NULL
and uid in (select uid,nick_name
from user_info u
where nick_name like '牛客%号' or nick_name like '[0-9]') 
and exam_id in (select 	exam_id,*
from examination_info
where tag like 'c%' or tag like 'C%'
and submit_time is not NULL)
group by uid

正确代码:

select uid,exam_id,round(sum(score)/count(exam_id),0) as avg_score
from exam_record
left join user_info using(uid)
left join examination_info using(exam_id)
where nick_name regexp '^牛客[0-9]+号$|^[0-9]+$'
      and tag regexp '^[Cc]'
      and submit_time is not NULL
group by uid,exam_id
order by 1,3

复盘:

(1)'牛客%号',中间的%不只是数字,所以正确应该是'^牛客[0-9]+号$',

纯数字也不是'[0-9]',而是'^[0-9]+$'。

(2)正则表达式函数用 RLIKE 或者 REGEXP

学习链接:Mysql正则表达式查询(含完整例子)_查询name字段包含字母的数据-CSDN博客

由于"牛客"+纯数字+"号"和纯数字是并集,所以要用()括起来
“^牛客[0-9]+号$ ”:意思是以牛客开头“^牛客”,匹配0-9任意字符一次“[0-9]+”,号结尾“号$”,

“^[0-9]+$”:以任意数字开头结尾的用户。

(3)like 和 rlike 的区别

  1). like

    1. sql语法的 模糊匹配

        2. 通配符

      1. % 代表零个或任意字符

      2. _ 代表1个字符

   2). like

  1. hive 扩展功能, 通过 Java 正则表达式 来匹配条件

(4)还有一种方法是 regexp:

where nick_name regexp '^牛客[0-9]+号$|^[0-9]+$'

      and tag regexp '^[Cc]'

(5)重要区别

like 整个字段匹配表达式成功才返回
regexp 部分字符匹配表达式成功即可返回

  • 20
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis SQL语句的进阶主要包括以下几个方面: 1. 通过查询时自定义别名的方式解决名称不一致而导致的无法封装数据的问题。在查询语句中,可以使用AS关键字为查询结果的列指定别名,从而解决结果集与实体类属性名称不一致的问题。 2. 使用resultMap节点解决名称不一致而导致的无法封装数据的问题。resultMap节点可以在映射文件中定义,用于将查询结果映射到实体类中。通过定义resultMap节点,我们可以灵活地指定查询结果列与实体类属性之间的对应关系,从而解决名称不一致的问题。 3. 一对一的关联查询。在MyBatis中,可以通过嵌套查询或者使用association标签来实现一对一的关联查询。嵌套查询是指在resultMap中定义一个嵌套的resultMap,通过association标签来指定关联关系。这样可以在查询数据的同时,将关联的数据一并查询出来并映射到实体类中。 4. 一对多的关联查询。在MyBatis中,可以通过collection标签来实现一对多的关联查询。collection标签可以在resultMap中定义,用于指定集合属性的映射关系。通过定义collection标签,我们可以在查询数据的同时,将关联的多个数据一并查询出来并映射到实体类中。 5. 通过使用动态SQL来实现更灵活的查询。MyBatis提供了丰富的动态SQL语法,在查询时可以根据条件进行判断、循环等操作,从而实现更灵活的查询。比如可以使用if标签、choose标签、foreach标签等来动态拼接查询条件,以满足不同的查询需求。 总结:MyBatis SQL语句的进阶包括通过自定义别名、使用resultMap节点解决名称不一致的问题,实现一对一和一对多的关联查询,以及使用动态SQL实现更灵活的查询。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值