SQL循環語句

前言

有時咱們須要添加不少測試的數據 在不使用Java的狀況下 如何用SQL語句進行添加 ? 本文將講述單層循環,雙層循環,以及日期循環mysql

image

單層循環

測試表代碼sql

create table test(location_id int ,location_name varchar(10));

再編寫存儲過程,其中涉及到循環的使用。咱們欲經過這個存儲過程,來達到往表中插入數據的效果函數

drop procedure if exists insert_while;
delimiter //
create procedure insert_while()
begin
    declare i int default 1;
    while i<10
    do
        insert into test values(i,concat('bookworm',i));
        set i=i+1;
    end while;
    commit;
end //
delimiter ;

下面是輸出結果:oop

mysql> select * from test;
Empty set (0.00 sec)

mysql> call insert_while();
Query OK, 0 rows affected (0.35 sec)

mysql> select * from test;
+-------------+---------------+
| location_id | location_name |
+-------------+---------------+
|           1 | bookworm1     |
|           2 | bookworm2     |
|           3 | bookworm3     |
|           4 | bookworm4     |
|           5 | bookworm5     |
|           6 | bookworm6     |
|           7 | bookworm7     |
|           8 | bookworm8     |
|           9 | bookworm9     |
+-------------+---------------+
9 rows in set (0.00 sec)

drop procedure if exists insert_while:若是存在函數insertwhile先刪除它測試

delimiter //:將結束符定義爲//在begin和end中會使用;若是不換程序會錯誤spa

create procedure:建立存儲過程code

delimiter ;結束符換回;號orm

雙重循環

建立表對象

CREATE TABLE `dim_time` (
  `TimeKey` int(11) NOT NULL,
  `Hour` tinyint(4) DEFAULT NULL,
  `Minute` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`TimeKey`)
)

編寫存儲過程blog

drop procedure if exists insertValueIntoDimTime;
delimiter //
create procedure insertValueIntoDimTime()
begin
    declare hour int default 0;
    declare min int default 0;    
        while hour < 24
        do
            while min < 60
            do
                insert into dim_time values(hour*100+min,hour,min);
                set min=min+1;
            end while;
            set min = 0;
            set hour = hour+1;
        end while;
end //
delimiter ;

運行

mysql> call insertValueIntoDimTime;
Query OK, 1 row affected (22.12 sec)

結果

mysql> select count(*) from dim_time;
+----------+
| count(*) |
+----------+
|     1440 |
+----------+
1 row in set (0.02 sec)

mysql> select * from dim_time order by timekey limit 10;
+---------+------+--------+
| TimeKey | Hour | Minute |
+---------+------+--------+
|       0 |    0 |      0 |
|       1 |    0 |      1 |
|       2 |    0 |      2 |
|       3 |    0 |      3 |
|       4 |    0 |      4 |
|       5 |    0 |      5 |
|       6 |    0 |      6 |
|       7 |    0 |      7 |
|       8 |    0 |      8 |
|       9 |    0 |      9 |
+---------+------+--------+
10 rows in set (0.00 sec)

mysql> select * from dim_time order by timekey desc limit 10;
+---------+------+--------+
| TimeKey | Hour | Minute |
+---------+------+--------+
|    2359 |   23 |     59 |
|    2358 |   23 |     58 |
|    2357 |   23 |     57 |
|    2356 |   23 |     56 |
|    2355 |   23 |     55 |
|    2354 |   23 |     54 |
|    2353 |   23 |     53 |
|    2352 |   23 |     52 |
|    2351 |   23 |     51 |
|    2350 |   23 |     50 |
+---------+------+--------+
10 rows in set (0.00 sec)

日期循環

3.1 需求
今天須要從訂單庫中找出過去六個月的訂單,因此須要寫一個簡單的日期循環,用於讀取數據。

3.2實現

drop procedure if exists date_loop;

delimiter //
create procedure date_loop()
begin
    declare i int default 1;
    declare start_date date ;  -- 當前日子減去6個月
    declare end_date date ; -- 當前天
    
    select date_sub(current_date(),interval 6 month) into start_date;
    select current_date() into end_date;
    
    while start_date < end_date
    do
        select start_date;
        set start_date = date_add(start_date,interval 1 day);
    end while;
    commit;
end //
delimiter ;

date_sub(object,interval):從指定日期減去指定年,月,日,時,分,秒

object:

  • 必須
  • 規定一個由data_current()返回的DataTime的對象

interval:

  • 必須
  • 規定是一個interval對象

返回值:若是成功返回DataTime對象,若是失敗返回false

image

要是能爲您提供幫助,請給予支持(關注、點贊、分享),蟲蟲蟹蟹你們了!

相關文章
相關標籤/搜索