Mysql 控制結構初識

来源:https://www.cnblogs.com/chenjieyouge/archive/2019/10/03/11621125.html
-Advertisement-
Play Games

Mysql 流程式控制制 認識 從我目前所接觸的編程語言,C, R, VB, Python, Javascript...,來看, 無非就是 變數, 表達式, 流程式控制制(順序, 分支, 迴圈), 封裝了一些更高級的數據結構而已 , 區別在於應用場景和語言特性, 其實邏輯都是相同的, 唯手熟爾. 選擇結構 ...


Mysql 流程式控制制

認識

從我目前所接觸的編程語言,C, R, VB, Python, Javascript...,來看, 無非就是變數, 表達式, 流程式控制制(順序, 分支, 迴圈), 封裝了一些更高級的數據結構而已, 區別在於應用場景和語言特性, 其實邏輯都是相同的, 唯手熟爾.

選擇結構 if-else; case

-- if-esle 語法
IF search_condition THEN 
    statement_list;
[ELSEIF search_condition THEN
    statement_list; ....]
ELSE
    statement_list;
END IF;
-- CASE 語法
CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list]...
    [ELSE statement_list]
END CASE;

OR:
CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE
-- 隨機推送一句表白
drop procedure if exists sayLove;
delimiter //
create procedure sayLove()
begin
    declare num int default 0;
    -- 生成一個1-5間的隨機數
    set num := round(rand()*5);
    -- 判斷
    case num
        when 1 then select "人生若只如初見";
        when 2 then select "春風十里不如你";
        when 3 then select "愛你就像愛生命";
        else
            select "今晚的月色真美";
    end case;
end //
delimiter ;

call sayLove();

-- out
mysql> call sayLove();
+----------------+
| 今晚的月色真美 |
+----------------+
| 今晚的月色真美 |
+----------------+
1 row in set (0.09 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call sayLove();
+----------------+
| 愛你就像愛生命 |
+----------------+
| 愛你就像愛生命 |
+----------------+
1 row in set (0.14 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call sayLove();
+----------------+
| 春風十里不如你 |
+----------------+
| 春風十里不如你 |
+----------------+
1 row in set (0.11 sec)

CASE 能實現的, IF-ELSE也完全能, 只是提供了更多的選擇而已.

-- 用 if-esle實現
drop procedure if exists sayLove;
delimiter //
create procedure sayLove()
begin
    declare num int default 0;
    -- 生成一個1-5間的隨機數
    set num := round(rand()*5);
    -- 判斷
    if num=1 then select "人生若只如初見";
    elseif num=2 then select "春風十里不如你";
    elseif num-3 then select "愛你就像愛生命";
    else
        select "今晚的月色真美";
    end if;
end //
delimiter ;

call sayLove();

MySql 迴圈

  • WHILE ... DO ... END WHILE 叫什麼"當"型迴圈, 滿足條件才進入迴圈體
  • LOOP ... LEAVE...END LOOP "直到型迴圈"
  • REPEAT ... UNTIL ... END REPEAT

while ...do ...迴圈

while search_condition do
    statement_list;
end while;

repeat ...until ...迴圈

repeat
    statement_list;
until search_condition;
end repeat;

loop ...leave 迴圈

[begin_label:] loop
    statement_list;
    leave [begin_label];
end loop [end_label];

迴圈-案例 1+2+...n

-- while 實現 求1+2+3+..n和
-- 自己容易混的點: 忘在結尾; end; 變數忘了 set;
-- 傳入參數: in 傳入值; out: 傳入變數去接收返回的值; inout 傳入又輸出
drop procedure if exists sumN_while;
delimiter //
create procedure sumN_while(in n int)
begin
    declare total int default 0;
    declare i int default 0;
    -- while ...do ....
    while i <= n do
        set total := total + i;
        set i := i + 1;
    -- 列印一下結果
    end while;
    select concat("1+2+..", n, "的和是:", total) as '輸出啦';
end //
delimiter ;

call sumN_while(100);

-- out
call sumN_while(100);
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+----------------------+
| 輸出啦               |
+----------------------+
| 1+2+..100的和是:5050 |
+----------------------+
1 row in set (0.10 sec)

同樣同 repeat ... until ..實現, 順便練習下 out 類型參數

-- repeat 實現 1+2+..n的和
drop procedure if exists sumN_repeat;
delimiter //
-- 設置傳入out型參數變數, 用來接收輸出值
create procedure sumN_repeat(out total int)
begin
    xxxx
end //
delimiter ;
drop procedure if exists sumN_repeat;
delimiter //
-- 設置再傳入out型參數變數, 用來接收輸出值
create procedure sumN_repeat(in n int)
begin
    declare i int default 0;
    declare total int default 0;
    
    -- repeat ... until ...
    repeat
        set total := total + i;
        set i := i + 1;
        -- 退出條件 until..True時才退出哦, 註意跟while的區別
        until i > n
    end repeat;
    -- 在內部列印出結果
    select total;
end //
delimiter ;

-- out
call sumN_repeat(100);
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+-------+
| total |
+-------+
|  5050 |
+-------+
1 row in set (0.09 sec)

用out類型參數.

-- repeat 實現 1+2+..n的和
drop procedure if exists sumN_repeat;
delimiter //
-- 設置再傳入out型參數變數, 用來接收輸出值
create procedure sumN_repeat(in n int, out total int)
begin
    declare i int default 0;
    
    set total := 0;  -- 順序: 先decalre 再是set, 不能亂,兄弟
    -- repeat ... until ...
    repeat
        set total := total + i;
        set i := i + 1;
        -- 退出條件 until..註意是條件為True時退出哦
        until i > n
    end repeat;
end //
delimiter ;

-- set @ret := 0;
-- call sumN_repeat(100, @ret);
-- select @ret;

-- out
mysql> set @ret := 0;  -- 這個全局變數 @ret 用來接收過程的 total值哦 
Query OK, 0 rows affected (0.00 sec)

mysql> call sumN_repeat(10000, @ret);
Query OK, 0 rows affected (0.04 sec)

mysql> select @ret;
+----------+
| @ret     |
+----------+
| 50005000 |
+----------+
1 row in set (0.08 sec)

再用 loop....leave 來整一波

-- loop ...leave ... 來實現 求 1+2+..n 的和
drop procedure if exists sumN_loop;
delimiter //
create procedure sumN_loop(in n int, out total int)
begin
    declare i int default 0;
    set total := 0;
    -- loop, 先取一個標簽名, 再寫退出條件, if-then...
    myLoop: loop
        if i > n then
            leave myLoop;
        end if;
        set total := total + i;
        set i := i + 1;
    end loop;
end //
delimiter ;

-- out
mysql> set @ret := 0;
Query OK, 0 rows affected (0.00 sec)

mysql> call sumN_loop(100, @ret);
Query OK, 0 rows affected (0.00 sec)

mysql> select @ret;
+------+
| @ret |
+------+
| 5050 |
+------+
1 row in set (0.11 sec)

小結MySql控制流

補充: 存儲過程的參數聲明

  • in 類型: 要求在調用的時候, 接收從外界傳入一個值.
  • out 類型: 要求在調用時, 傳入一個變數去接收procedure的"返回值"
  • inout 類型: 輸入輸出型

補充: MySql變數定義

  • 在存儲過程內, 用: declare 變數名 類型 [default 值]; 類似"局部變數"
  • 在外邊運行, 用: @變數 := 值; 類似"全局變數", 註意MySql的標準賦值符號是 " := ", 而 "=" 只有在update 和set時表示賦值, 其餘場景都是 "等號".
  • 選擇結構(if, case):
    • if - elseif- esle -end if;
    • case value when value1 then ; when value2 ...then .. else .. . end case;
  • 迴圈結構(while, repeat, loop)
    • while .... do .... end while;
    • repeat ... until .... end repeat;
    • myLoop: loop ..... if ... then leave myLoop; end if ; ..... end loop;

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1.Autofac基礎使用 參考: https://www.cnblogs.com/li150dan/p/10071079.html 2.ASP.NETCore 3.0 Autofac 容器替換 需要引用:Autofac, Autofac.Extensions.DependencyInjection ...
  • 更改Ubuntu下預設Python版本 首先查看系統內有哪些版本的Python ls /usr/bin/python 查看當前python版本 python --version 基於用戶修改預設版本 想要為某個特定用戶修改 Python 版本,只需要在其 home 目錄下創建一個 alias(別名) ...
  • vim 功能 : 一個強大的文本編輯器 語法格式 :vim [ 選項 ] / 路徑 / 文本文件名 命令格式: vi [ 選項 ] [ 文件名 ] +num 打開某個文件直接跳轉到 num 行 -b 以 binary 方式打開文件 , 用於編輯二進位文件 -R 以只讀方式打開文件 一.VIM 基礎使 ...
  • Vsftp 實驗案例一:(本地用戶) 試驗版本:Linux7.X版本 公司內部現在有一臺FTP 和WEB 伺服器,FTP 的功能主要用於維護公司的網站內容,包括上傳文 件、創建目錄、更新網頁等等。公司現有兩個部門負責維護任務,他們分別適用team1 和team2 帳號進行管理。先要求僅允許team1 ...
  • 1、檢查環境 2、配置yum源 mkdir /iso #創建掛載點 mount /dev/cdrom /iso #掛載光碟到掛載點 ls /iso #查看掛載是否成功 cd /etc/yum.repos.d/ #進入yum源目錄 rm -fr *.repo #刪除官方源 vim iso.repo # ...
  • RAID是將把好幾塊硬碟通過一定組合方式把它組合起來,成為一個新的硬碟陣列組,從而使它能夠達到高性能硬碟的要求. ...
  • MySQL複習值代碼知識點 一. 創建資料庫 create database 資料庫名; 二. 刪除資料庫 drop database 資料庫名; 三. 選擇相應的資料庫 use 資料庫名; 四. 創建表 create table table_name( id integer primary key ...
  • 外鍵(Foreign Key) 如果今天有一張表上面有很多職務的信息 我們可以通過使用外鍵的方式去將兩張表產生關聯 這樣的好處能夠節省空間,比方說你今天的職務名稱很長,在一張表中就要重覆的去寫這個職務的名字,很浪費空間;除此之外也能起到一個約束的作用。 像department就是外鍵。 執行代碼: ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...