码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 2205,2228,2230,2238,2292


    本节涉及:function函数(2205)、存储过程(2230)、自连接(2228、2292)和delimiter结束符


    文章目录

    • 2205. The Number of Users That Are Eligible for Discount(function函数)
    • 2228. Users With Two Purchases Within Seven Days(自连接)
    • 2230. 查找可享受优惠的用户(存储过程)
    • 2238. Number of Times a Driver Was a Passenger
    • 2292. Products With Three or More Orders in Two Consecutive Years
    • 总结
      • mysql:函数(function)
        • 创建无参function函数
        • 创建有参function函数
        • 使用function函数
        • 删除function函数
      • MySQL存储过程
        • 创建存储过程(in类型输入,out输出,inout输入即输出)
        • 存储过程内定义变量和该表变量值
        • 使用存储过程
        • 删除存储过程
        • 存储过程注意事项(下方是自定义[delimiter]结束符)


    2205. The Number of Users That Are Eligible for Discount(function函数)

    在这里插入图片描述

    CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT
    BEGIN
      RETURN (
          # Write your MySQL query statement below.
          select count(distinct user_id)
          from Purchases
          where time_stamp between startDate and endDate
          and amount >= minAmount
          
      );
    END
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2228. Users With Two Purchases Within Seven Days(自连接)

    在这里插入图片描述

    # Write your MySQL query statement below
    select distinct b.user_id
    from Purchases a,Purchases b
    where a.user_id = b.user_id and
          a.purchase_id <> b.purchase_id and
          abs(datediff(a.purchase_date,b.purchase_date)) <= 7
    order by b.user_id 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2230. 查找可享受优惠的用户(存储过程)

    在这里插入图片描述在这里插入图片描述

    CREATE PROCEDURE getUserIDs(startDate DATE, endDate DATE, minAmount INT)
    BEGIN
    	# Write your MySQL query statement below.
        select distinct user_id
        from Purchases
        where time_stamp between startDate and endDate
              and amount >= minAmount
        order by user_id;
    END
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2238. Number of Times a Driver Was a Passenger

    在这里插入图片描述

    # Write your MySQL query statement below
    select distinct driver_id,ifnull(zongshu,0) cnt
    from Rides R
    left join (
    select passenger_id ,count(*) zongshu
    from Rides
    group by passenger_id)new_table
    on R.driver_id = new_table.passenger_id
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2292. Products With Three or More Orders in Two Consecutive Years

    Write an SQL query to report the IDs of all the products that were ordered three or more times in two consecutive years.
    Return the result table in any order.
    The query result format is shown in the following example.
    Example 1:
    Input:
    Orders table:
    ±---------±-----------±---------±--------------+
    | order_id | product_id | quantity | purchase_date |
    ±---------±-----------±---------±--------------+
    | 1 | 1 | 7 | 2020-03-16 |
    | 2 | 1 | 4 | 2020-12-02 |
    | 3 | 1 | 7 | 2020-05-10 |
    | 4 | 1 | 6 | 2021-12-23 |
    | 5 | 1 | 5 | 2021-05-21 |
    | 6 | 1 | 6 | 2021-10-11 |
    | 7 | 2 | 6 | 2022-10-11 |
    ±---------±-----------±---------±--------------+
    Output:
    ±-----------+
    | product_id |
    ±-----------+
    | 1 |
    ±-----------+
    Explanation:
    Product 1 was ordered in 2020 three times and in 2021 three times. Since it was ordered three times in two consecutive years, we include it in the answer.
    Product 2 was ordered one time in 2022. We do not include it in the answer.

    with new_table as(
    select product_id,count(date_format(purchase_date,'%Y')) zongshu,
    date_format(purchase_date,'%Y') riqi
    from Orders
    group by product_id , date_format(purchase_date,'%Y')
    order by product_id ,purchase_date
    )
    select distinct n2.product_id
    from new_table n1 ,new_table n2
    where n1.product_id = n2.product_id and
          n1.riqi-n2.riqi = -1 and
          n1.zongshu >=3 and 
          n2.zongshu>=3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    总结

    mysql:函数(function)

    创建无参function函数

    creat function fun_name() return int
    #注意int是返回类型,可以是date
    (
    begin
    【SQL语句】
    end
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    创建有参function函数

    creat function fun_name(age int ,name  varchar(10)) return int
    #注意int是返回类型,可以是date
    (
    begin
    【SQL语句】
    end
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用function函数

    SHOW CREATE FUNCTION fun_name(10,'Alice');
    
    • 1

    删除function函数

    drop function fun_name;
    
    • 1

    MySQL存储过程

    创建存储过程(in类型输入,out输出,inout输入即输出)

    create procedure p3(in n int)
    begin
        declare total int default 0;
        while n>0 do
                set total := total + n;
                set n := n -1;
            end while;
        select total;
    end;
    
    create procedure p4_11(inout score int)
    begin
        declare  n int default 100;
        repeat
            set score:=score+n;
            set n:=n-1;
        until n <= 0
        end repeat;
    end;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    存储过程内定义变量和该表变量值

    定义(declear)
    declare total int default 0;
    改变(set)
    set a := 10;
    
    • 1
    • 2
    • 3
    • 4

    使用存储过程

    call p3(100);
    
    set @score=0;
    call p4_11(@score);
    select @score;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    删除存储过程

    drop procedure p4_11;
    
    • 1

    存储过程注意事项(下方是自定义[delimiter]结束符)

    mysql结束符是;
    因为存储函数end结尾前需要有结束符 ; 并表示结束,
    在linux中要自定义个其他结束符表示这个函数并没有写完,
    所以要自定义结束符

    DELIMITER ¥¥
    
    • 1
  • 相关阅读:
    理解什么是接口测试?怎样做接口测试?
    里氏替换原则~
    华为P系列“砍了”,三角美学系列全新登场
    从事软件开发工作的一些感悟
    INDEMIND:渗透率不足10%,扫地机器人为何成了“存量市场”?
    通过安全的云开发环境重新发现 DevOps 的心跳
    MySQL告警“Connection attributes of length 570 were truncated“
    阿里云服务操作指南-个人购买版
    Jenkins
    2022/11/12 题目练习
  • 原文地址:https://blog.csdn.net/weixin_45646601/article/details/127595780
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号