44 - 50题高级字符串函数 / 正则表达式 / 子句 - 高频 SQL 50 题基础版

目录

  • [1. 相关知识点](#1. 相关知识点)
  • 2.例子
    • [2.44 - 修复表中的名字](#2.44 - 修复表中的名字)
    • [2.45 - 患某种疾病的患者](#2.45 - 患某种疾病的患者)
    • [2.46 - 删除重复的电子邮箱](#2.46 - 删除重复的电子邮箱)
    • [2.47 - 第二高的薪水](#2.47 - 第二高的薪水)
    • [2.48 - 按日期分组销售产品](#2.48 - 按日期分组销售产品)
    • [2.49 - 列出指定时间段内所有的下单产品](#2.49 - 列出指定时间段内所有的下单产品)
    • [2.50 - 查找拥有有效邮箱的用户](#2.50 - 查找拥有有效邮箱的用户)

1. 相关知识点

  • 相关函数

    函数 含义
    concat() 字符串拼接
    upper() 转大写
    lower() 转小写
    left(name,1) 从左边截取
    right(name,1) 从有边截取
    substr(name,2) 从右边第二个位置开始截取
    group_concat(name separator ',') 分组后所有的name根据','拼接
    regexp 正则表达式筛选条件
  • 指定2020年2月的方法

    • order_date between '2020-02-01' and '2020-02-29'
    • order_date like '2020-02%'
    • DATE_FORMAT(order_date, "%Y-%m") = "2020-02"
    • LEFT(order_date, 7) 或 substr(1, 7)

2.例子

2.44 - 修复表中的名字


sql 复制代码
-- concat(upper(left(name,1)),lower(right(name,length(name)-1))) 
-- concat(upper(left(name,1)),lower(substr(name,2)))

select
    user_id,
    concat(upper(left(name,1)),lower(right(name,length(name)-1))) as name
from
    users
order by
    user_id;

2.45 - 患某种疾病的患者


sql 复制代码
select
    *
from
    Patients
where
    conditions  like 'DIAB1%' or conditions  like '% DIAB1%'

2.46 - 删除重复的电子邮箱


sql 复制代码
delete 
    p1
from 
    Person p1,Person p2
where 
    p1.email=p2.email and p1.id>p2.id;

2.47 - 第二高的薪水



sql 复制代码
-- 方法一
select(
    select
        salary
    from   
        Employee
    order by
        salary
    limit 1 offset 1) as SecondHighestSalary;

-- 方法二
select
    (select
        salary 
    from
        (
            select
                *,
                row_number() over(order by salary desc) number
            from
                Employee 
            
        ) as t
    where
        number=2) as SecondHighestSalary ;

2.48 - 按日期分组销售产品



sql 复制代码
-- group_concat 分组拼接

select
    sell_date,
    count(distinct product) num_sold,
    group_concat(distinct product order by product  separator ',') products       
from
    Activities
group by
    sell_date;      

2.49 - 列出指定时间段内所有的下单产品





sql 复制代码
-- 指定2020年2月的方法
-- (1) order_date between '2020-02-01' and '2020-02-29'  
-- (2) order_date like '2020-02%'  
-- (3) DATE_FORMAT(order_date, "%Y-%m") = "2020-02"   
-- (4) LEFT(order_date, 7) 或 substr(1, 7)

select
    p.product_name,sum(unit) unit
from
    Orders o left join Products p on p.product_id=o.product_id
where
    o.order_date like '%2020-02%'
group by
    o.product_id
having
    sum(unit)>=100

2.50 - 查找拥有有效邮箱的用户



sql 复制代码
select
    *
from
    Users 
where
    mail regexp '^[A-Za-z][a-zA-Z0-9\\_\\.\\-\\/]*@leetcode\.com$'
相关推荐
极小狐1 小时前
极狐GitLab 容器镜像仓库功能介绍
java·前端·数据库·npm·gitlab
极小狐1 小时前
如何构建容器镜像并将其推送到极狐GitLab容器镜像库?
开发语言·数据库·机器学习·gitlab·ruby
阿四啊1 小时前
【Redis实战篇】分布式锁-Redisson
数据库·redis·分布式
_星辰大海乀2 小时前
数据库约束
java·数据结构·数据库·sql·链表
多多*2 小时前
Java反射 八股版
java·开发语言·hive·python·sql·log4j·mybatis
一只fish2 小时前
MySQL 8.0 OCP 1Z0-908 题目解析(1)
数据库·mysql
AI大模型顾潇2 小时前
[特殊字符] 本地部署DeepSeek大模型:安全加固与企业级集成方案
数据库·人工智能·安全·大模型·llm·微调·llama
FAQEW2 小时前
MongDB和MySQL的区别
数据库·mysql·mongdb·区别
码农飞哥2 小时前
互联网大厂Java面试实战:Spring Boot到微服务的技术问答解析
java·数据库·spring boot·缓存·微服务·消息队列·面试技巧
niechel3 小时前
02-GBase 8s 事务型数据库 客户端工具dbaccess
数据库