亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MySQL 游標(biāo)的定義與使用方式

 更新時間:2021年01月21日 16:04:03   作者:幽冥狂_七  
這篇文章主要介紹了MySQL 游標(biāo)的定義與使用方式,幫助大家更好的理解和使用MySQL,感興趣的朋友可以了解下

創(chuàng)建游標(biāo)

首先在MySql中創(chuàng)建一張數(shù)據(jù)表:

CREATE TABLE IF NOT EXISTS `store` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) NOT NULL,
 `count` int(11) NOT NULL DEFAULT '1',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7;
 
INSERT INTO `store` (`id`, `name`, `count`) VALUES
(1, 'android', 15),
(2, 'iphone', 14),
(3, 'iphone', 20),
(4, 'android', 5),
(5, 'android', 13),
(6, 'iphone', 13);

我們現(xiàn)在要用存儲過程做一個功能,統(tǒng)計iphone的總庫存是多少,并把總數(shù)輸出到控制臺。

--在windows系統(tǒng)中寫存儲過程時,如果需要使用declare聲明變量,需要添加這個關(guān)鍵字,否則會報錯。
delimiter //
drop procedure if exists StatisticStore;
CREATE PROCEDURE StatisticStore()
BEGIN
  --創(chuàng)建接收游標(biāo)數(shù)據(jù)的變量
  declare c int;
  declare n varchar(20);
  --創(chuàng)建總數(shù)變量
  declare total int default 0;
  --創(chuàng)建結(jié)束標(biāo)志變量
  declare done int default false;
  --創(chuàng)建游標(biāo)
  declare cur cursor for select name,count from store where name = 'iphone';
  --指定游標(biāo)循環(huán)結(jié)束時的返回值
  declare continue HANDLER for not found set done = true;
  --設(shè)置初始值
  set total = 0;
  --打開游標(biāo)
  open cur;
  --開始循環(huán)游標(biāo)里的數(shù)據(jù)
  read_loop:loop
  --根據(jù)游標(biāo)當(dāng)前指向的一條數(shù)據(jù)
  fetch cur into n,c;
  --判斷游標(biāo)的循環(huán)是否結(jié)束
  if done then
    leave read_loop;  --跳出游標(biāo)循環(huán)
  end if;
  --獲取一條數(shù)據(jù)時,將count值進行累加操作,這里可以做任意你想做的操作,
  set total = total + c;
  --結(jié)束游標(biāo)循環(huán)
  end loop;
  --關(guān)閉游標(biāo)
  close cur;
 
  --輸出結(jié)果
  select total;
END;
--調(diào)用存儲過程
call StatisticStore();

fetch是獲取游標(biāo)當(dāng)前指向的數(shù)據(jù)行,并將指針指向下一行,當(dāng)游標(biāo)已經(jīng)指向最后一行時繼續(xù)執(zhí)行會造成游標(biāo)溢出。
使用loop循環(huán)游標(biāo)時,他本身是不會監(jiān)控是否到最后一條數(shù)據(jù)了,像下面代碼這種寫法,就會造成死循環(huán);

read_loop:loop
fetch cur into n,c;
set total = total+c;
end loop;

在MySql中,造成游標(biāo)溢出時會引發(fā)mysql預(yù)定義的NOT FOUND錯誤,所以在上面使用下面的代碼指定了當(dāng)引發(fā)not found錯誤時定義一個continue 的事件,指定這個事件發(fā)生時修改done變量的值。

declare continue HANDLER for not found set done = true;

所以在循環(huán)時加上了下面這句代碼:

--判斷游標(biāo)的循環(huán)是否結(jié)束
if done then
  leave read_loop;  --跳出游標(biāo)循環(huán)
end if;

如果done的值是true,就結(jié)束循環(huán)。繼續(xù)執(zhí)行下面的代碼。

使用方式

游標(biāo)有三種使用方式:
第一種就是上面的實現(xiàn),使用loop循環(huán);
第二種方式如下,使用while循環(huán):

drop procedure if exists StatisticStore1;
CREATE PROCEDURE StatisticStore1()
BEGIN
  declare c int;
  declare n varchar(20);
  declare total int default 0;
  declare done int default false;
  declare cur cursor for select name,count from store where name = 'iphone';
  declare continue HANDLER for not found set done = true;
  set total = 0;
  open cur;
  fetch cur into n,c;
  while(not done) do
    set total = total + c;
    fetch cur into n,c;
  end while;
  
  close cur;
  select total;
END;
 
call StatisticStore1();

第三種方式是使用repeat執(zhí)行:

drop procedure if exists StatisticStore2;
CREATE PROCEDURE StatisticStore2()
BEGIN
  declare c int;
  declare n varchar(20);
  declare total int default 0;
  declare done int default false;
  declare cur cursor for select name,count from store where name = 'iphone';
  declare continue HANDLER for not found set done = true;
  set total = 0;
  open cur;
  repeat
  fetch cur into n,c;
  if not done then
    set total = total + c;
  end if;
  until done end repeat;
  close cur;
  select total;
END;
 
call StatisticStore2();

游標(biāo)嵌套

在mysql中,每個begin end 塊都是一個獨立的scope區(qū)域,由于MySql中同一個error的事件只能定義一次,如果多定義的話在編譯時會提示Duplicate handler declared in the same block。

drop procedure if exists StatisticStore3;
CREATE PROCEDURE StatisticStore3()
BEGIN
  declare _n varchar(20);
  declare done int default false;
  declare cur cursor for select name from store group by name;
  declare continue HANDLER for not found set done = true;
  open cur;
  read_loop:loop
  fetch cur into _n;
  if done then
    leave read_loop;
  end if;
  begin
    declare c int;
    declare n varchar(20);
    declare total int default 0;
    declare done int default false;
    declare cur cursor for select name,count from store where name = 'iphone';
    declare continue HANDLER for not found set done = true;
    set total = 0;
    open cur;
    iphone_loop:loop
    fetch cur into n,c;
    if done then
      leave iphone_loop;
    end if;
    set total = total + c;
    end loop;
    close cur;
    select _n,n,total;
  end;
  begin
      declare c int;
      declare n varchar(20);
      declare total int default 0;
      declare done int default false;
      declare cur cursor for select name,count from store where name = 'android';
      declare continue HANDLER for not found set done = true;
      set total = 0;
      open cur;
      android_loop:loop
      fetch cur into n,c;
      if done then
        leave android_loop;
      end if;
      set total = total + c;
      end loop;
      close cur;
    select _n,n,total;
  end;
  begin
  
  end;
  end loop;
  close cur;
END;
 
call StatisticStore3();

上面就是實現(xiàn)一個嵌套循環(huán),當(dāng)然這個例子比較牽強。湊合看看就行。。

動態(tài)SQL

Mysql 支持動態(tài)SQL的功能,

set @sqlStr='select * from table where condition1 = ?';
prepare s1 for @sqlStr;
--如果有多個參數(shù)用逗號分隔
execute s1 using @condition1;
--手工釋放,或者是 connection 關(guān)閉時, server 自動回收
deallocate prepare s1;

以上就是MySQL 游標(biāo)的定義與使用方式的詳細內(nèi)容,更多關(guān)于MySQL 游標(biāo)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • MySQL COUNT(*)性能原理詳解

    MySQL COUNT(*)性能原理詳解

    這篇文章主要介紹了MySQL COUNT(*)性能原理詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • 圖文并茂地講解Mysql索引(index)

    圖文并茂地講解Mysql索引(index)

    在關(guān)系數(shù)據(jù)庫中,索引是一種單獨的、物理的數(shù)對數(shù)據(jù)庫表中一列或多列的值進行排序的一種存儲結(jié)構(gòu),下面這篇文章主要給大家介紹了關(guān)于Mysql索引(index)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • mysql修改數(shù)據(jù)庫編碼(數(shù)據(jù)庫字符集)和表的字符編碼的方法

    mysql修改數(shù)據(jù)庫編碼(數(shù)據(jù)庫字符集)和表的字符編碼的方法

    Mysql數(shù)據(jù)庫是一個開源的數(shù)據(jù)庫,應(yīng)用非常廣泛。以下是修改mysql數(shù)據(jù)庫的字符編碼的操作過程和將表的字符編碼轉(zhuǎn)換成utf-8的方法,需要的朋友可以參考下
    2014-03-03
  • 淺談MySQL聚簇索引

    淺談MySQL聚簇索引

    數(shù)據(jù)庫的索引從不同的角度可以劃分成不同的類型,聚簇索引便是其中一種。聚簇索引并不是一種單獨的索引類型,而是一種數(shù)據(jù)的存儲方式。本文詳細介紹了MySQL的聚簇索引,感興趣的同學(xué)可以參考閱讀
    2023-04-04
  • MySQL查詢條件中in會用到索引嗎

    MySQL查詢條件中in會用到索引嗎

    這篇文章主要給大家介紹了MySQL查詢條件中in會不會用到索引的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • mysql 8.0.12 安裝配置教程

    mysql 8.0.12 安裝配置教程

    這篇文章主要為大家詳細介紹了mysql 8.0.12安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Mysql的longblob字段插入數(shù)據(jù)問題解決

    Mysql的longblob字段插入數(shù)據(jù)問題解決

    在使用mysql的過程中,有個問題就是mysql的優(yōu)化,mysql中l(wèi)ongblob字段在5.5版本中默認的為1M,需要解決問題的朋友可以參考下
    2014-01-01
  • 淺談MySQL8.0 異步復(fù)制的三種方式

    淺談MySQL8.0 異步復(fù)制的三種方式

    這篇文章主要介紹了淺談MySQL8.0 異步復(fù)制的三種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • mysql函數(shù)IFNULL使用的及注意事項說明

    mysql函數(shù)IFNULL使用的及注意事項說明

    這篇文章主要介紹了mysql函數(shù)IFNULL使用的及注意事項說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • MySQL是如何處理排序的(最新推薦)

    MySQL是如何處理排序的(最新推薦)

    在MySQL的查詢中常常會用到order by和group by這兩個關(guān)鍵字,它們的相同點是都會對字段進行排序,那查詢語句中的排序是如何實現(xiàn)的呢,下面跟隨小編一起看看吧
    2024-05-05

最新評論