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$'
相关推荐
fen_fen2 小时前
Oracle建表语句示例
数据库·oracle
砚边数影4 小时前
数据可视化入门:Matplotlib 基础语法与折线图绘制
数据库·信息可视化·matplotlib·数据可视化·kingbase·数据库平替用金仓·金仓数据库
orange_tt4 小时前
Djiango配置Celery
数据库·sqlite
云小逸5 小时前
【nmap源码学习】 Nmap网络扫描工具深度解析:从基础参数到核心扫描逻辑
网络·数据库·学习
肉包_5115 小时前
两个数据库互锁,用全局变量互锁会偶发软件卡死
开发语言·数据库·c++
霖霖总总5 小时前
[小技巧64]深入解析 MySQL InnoDB 的 Checkpoint 机制:原理、类型与调优
数据库·mysql
此刻你6 小时前
常用的 SQL 语句
数据库·sql·oracle
それども7 小时前
分库分表的事务问题 - 怎么实现事务
java·数据库·mysql
·云扬·7 小时前
MySQL Binlog 配置指南与核心作用解析
数据库·mysql·adb
天空属于哈夫克37 小时前
Java 版:利用外部群 API 实现自动“技术开课”倒计时提醒
数据库·python·mysql