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$'
相关推荐
jiayou6420 小时前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
李广坤2 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
爱可生开源社区3 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1773 天前
《从零搭建NestJS项目》
数据库·typescript
加号34 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏4 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
李慕婉学姐4 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
百锦再4 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
tryCbest4 天前
数据库SQL学习
数据库·sql
jnrjian4 天前
ORA-01017 查找机器名 用户名 以及library cache lock 参数含义
数据库·oracle