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$'
相关推荐
lifewange16 小时前
如何查看本地的数据库里信息
数据库
PSLoverS16 小时前
Python如何实现测试场景编排_基于pytest的数据驱动组合策略
jvm·数据库·python
HalvmånEver16 小时前
MySQL事务(一)
linux·数据库·学习·mysql
%KT%16 小时前
Agent开发:自动查天气+景区推荐
linux·数据库·php
会编程的土豆16 小时前
MySQL 多表查询
开发语言·数据库·python·mysql
2403_8832610916 小时前
PHP调用Codex处理PHP特定语法【操作】
jvm·数据库·python
四方云16 小时前
MySQL 迁移到 Apache Doris 生产实践:从评估到落地的完整指南
数据库·mysql·apache
跨境技工小黎16 小时前
亚马逊数据抓取怎么做?2026最新实战教程
java·大数据·数据库
lifewange16 小时前
afinfo 表设计
数据库·sql·mysql
m0_4636722016 小时前
CSS如何定义可重用的阴影效果_通过CSS变量管理box-shadow参数
jvm·数据库·python