Hướng dẫn mysql procedure if else if - thủ tục mysql nếu khác nếu


Mysql & nbsp; nếu khác nếu khác thực hiện các câu lệnh dựa trên nhiều biểu thức cú pháp của nó như sau - IF ELSEIF ELSE execute the statements based on multiple expressions Its syntax is as follows −

IF expression THEN
   statements;
ELSEIF elseif-expression THEN
   elseif-statements;
… … … …
ELSE
  else-statements;
END IF;

Các tuyên bố phải kết thúc bằng một dấu chấm phẩy.

Để chứng minh việc sử dụng IF Otherif khác trong quy trình được lưu trữ của MySQL, chúng tôi đang tạo quy trình được lưu trữ sau dựa trên các giá trị, như được hiển thị bên dưới, của bảng có tên ‘Student_info, -IF ELSEIF ELSE statement within MySQL stored procedure, we are creating the following stored procedure which is based on the values, as shown below, of the table named ‘student_info’ −

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)

Truy vấn sau đây sẽ tạo một quy trình có tên ‘Coursedetails_if_elseif, có nếu khác các câu lệnh khác trong đó -IF ELSEIF ELSE statements in it −

mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)

Bây giờ, chúng ta có thể thấy kết quả bên dưới khi chúng ta gọi thủ tục này -

mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)

Hướng dẫn mysql procedure if else if - thủ tục mysql nếu khác nếu

Cập nhật vào ngày 22 tháng 6 năm 2020 05:45:27

  • Câu hỏi và câu trả lời liên quan
  • Làm thế nào MySQL nếu câu lệnh khác có thể được sử dụng trong một quy trình được lưu trữ?
  • Làm thế nào MySQL nếu câu lệnh có thể được sử dụng trong một quy trình được lưu trữ?
  • Làm thế nào có thể sử dụng câu lệnh trường hợp MySQL trong quy trình lưu trữ?
  • Làm thế nào có thể sử dụng câu lệnh LOOP MySQL trong một quy trình được lưu trữ?
  • Làm thế nào MySQL trong khi câu lệnh LOOP có thể được sử dụng trong quy trình lưu trữ?
  • Làm thế nào câu lệnh Loop lặp lại MySQL có thể được sử dụng trong quy trình lưu trữ?
  • Thực hiện nếu khác trong quy trình lưu trữ trong MySQL?
  • Làm thế nào các biến cục bộ có thể được sử dụng trong thủ tục lưu trữ MySQL?
  • Làm thế nào các biến người dùng có thể được sử dụng trong thủ tục lưu trữ MySQL?
  • PHP nếu khác
  • Nếu tuyên bố khác trong câu lệnh MySQL?
  • Làm thế nào để thực hiện chính xác kết thúc nếu câu lệnh trong quy trình lưu trữ MySQL?
  • Làm thế nào một thủ tục lưu trữ MySQL có thể gọi một thủ tục lưu trữ MySQL khác bên trong nó?
  • Làm thế nào chúng ta có thể thay đổi quy trình lưu trữ MySQL?
  • Làm thế nào chúng ta có thể bỏ một thủ tục lưu trữ MySQL?

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu cách sử dụng câu lệnh MySQL nếu thực thi một khối mã SQL dựa trên một điều kiện được chỉ định.: in this tutorial, you will learn how to use MySQL IF statement to execute a block of SQL code based on a specified condition.

Lưu ý rằng MySQL có hàm if () khác với câu lệnh

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
7 được mô tả trong hướng dẫn này.

Tuyên bố

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
7 có ba biểu mẫu: đơn giản ____ 19 & nbsp; câu lệnh, câu lệnh
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
0 và câu lệnh
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
1.

Mysql đơn giản mysql> Select * from student_info; +------+---------+------------+------------+ | id   | Name    | Address    | Subject    | +------+---------+------------+------------+ | 101  | YashPal | Amritsar   | History    | | 105  | Gaurav  | Jaipur     | Literature | | 125  | Raman   | Shimla     | Computers  | +------+---------+------------+------------+ 3 rows in set (0.00 sec)9 câu lệnh

Câu lệnh

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
9 cho phép bạn thực thi một tập hợp các câu lệnh SQL dựa trên một điều kiện được chỉ định. Sau đây minh họa cú pháp của câu lệnh
mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
9:

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)

Trong cú pháp này:

  • Đầu tiên, chỉ định một điều kiện để thực thi mã giữa
    mysql> Select * from student_info;
    +------+---------+------------+------------+
    | id   | Name    | Address    | Subject    |
    +------+---------+------------+------------+
    | 101  | YashPal | Amritsar   | History    |
    | 105  | Gaurav  | Jaipur     | Literature |
    | 125  | Raman   | Shimla     | Computers  |
    +------+---------+------------+------------+
    3 rows in set (0.00 sec)
    9 và
    mysql> DELIMITER // ;
    mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
        -> BEGIN
        -> DECLARE Sub Varchar(20);
        -> SELECT Subject INTO SUB
        -> FROM Student_info WHERE S_Subject = Subject;
        -> IF Sub = 'Computers' THEN
        -> SET S_Course = 'B.Tech(CSE)';
        -> ELSEIF Sub = 'History' THEN
        -> SET S_Course = 'Masters in History';
        -> ELSEIF Sub = 'Literature' THEN
        -> SET S_Course = 'Masters in English';
        -> END IF;
        -> END //
    Query OK, 0 rows affected (0.00 sec)
    6. Nếu
    mysql> DELIMITER // ;
    mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
        -> BEGIN
        -> DECLARE Sub Varchar(20);
        -> SELECT Subject INTO SUB
        -> FROM Student_info WHERE S_Subject = Subject;
        -> IF Sub = 'Computers' THEN
        -> SET S_Course = 'B.Tech(CSE)';
        -> ELSEIF Sub = 'History' THEN
        -> SET S_Course = 'Masters in History';
        -> ELSEIF Sub = 'Literature' THEN
        -> SET S_Course = 'Masters in English';
        -> END IF;
        -> END //
    Query OK, 0 rows affected (0.00 sec)
    7 đánh giá thành
    mysql> DELIMITER // ;
    mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
        -> BEGIN
        -> DECLARE Sub Varchar(20);
        -> SELECT Subject INTO SUB
        -> FROM Student_info WHERE S_Subject = Subject;
        -> IF Sub = 'Computers' THEN
        -> SET S_Course = 'B.Tech(CSE)';
        -> ELSEIF Sub = 'History' THEN
        -> SET S_Course = 'Masters in History';
        -> ELSEIF Sub = 'Literature' THEN
        -> SET S_Course = 'Masters in English';
        -> END IF;
        -> END //
    Query OK, 0 rows affected (0.00 sec)
    8, các câu lệnh giữa
    mysql> Select * from student_info;
    +------+---------+------------+------------+
    | id   | Name    | Address    | Subject    |
    +------+---------+------------+------------+
    | 101  | YashPal | Amritsar   | History    |
    | 105  | Gaurav  | Jaipur     | Literature |
    | 125  | Raman   | Shimla     | Computers  |
    +------+---------+------------+------------+
    3 rows in set (0.00 sec)
    9 và
    mysql> DELIMITER // ;
    mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
        -> BEGIN
        -> DECLARE Sub Varchar(20);
        -> SELECT Subject INTO SUB
        -> FROM Student_info WHERE S_Subject = Subject;
        -> IF Sub = 'Computers' THEN
        -> SET S_Course = 'B.Tech(CSE)';
        -> ELSEIF Sub = 'History' THEN
        -> SET S_Course = 'Masters in History';
        -> ELSEIF Sub = 'Literature' THEN
        -> SET S_Course = 'Masters in English';
        -> END IF;
        -> END //
    Query OK, 0 rows affected (0.00 sec)
    6 sẽ thực thi. Mặt khác, điều khiển được chuyển sang câu lệnh tiếp theo sau
    mysql> DELIMITER // ;
    mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
        -> BEGIN
        -> DECLARE Sub Varchar(20);
        -> SELECT Subject INTO SUB
        -> FROM Student_info WHERE S_Subject = Subject;
        -> IF Sub = 'Computers' THEN
        -> SET S_Course = 'B.Tech(CSE)';
        -> ELSEIF Sub = 'History' THEN
        -> SET S_Course = 'Masters in History';
        -> ELSEIF Sub = 'Literature' THEN
        -> SET S_Course = 'Masters in English';
        -> END IF;
        -> END //
    Query OK, 0 rows affected (0.00 sec)
    6.
  • Thứ hai, chỉ định mã sẽ thực thi nếu
    mysql> DELIMITER // ;
    mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
        -> BEGIN
        -> DECLARE Sub Varchar(20);
        -> SELECT Subject INTO SUB
        -> FROM Student_info WHERE S_Subject = Subject;
        -> IF Sub = 'Computers' THEN
        -> SET S_Course = 'B.Tech(CSE)';
        -> ELSEIF Sub = 'History' THEN
        -> SET S_Course = 'Masters in History';
        -> ELSEIF Sub = 'Literature' THEN
        -> SET S_Course = 'Masters in English';
        -> END IF;
        -> END //
    Query OK, 0 rows affected (0.00 sec)
    7 đánh giá thành
    mysql> DELIMITER // ;
    mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
        -> BEGIN
        -> DECLARE Sub Varchar(20);
        -> SELECT Subject INTO SUB
        -> FROM Student_info WHERE S_Subject = Subject;
        -> IF Sub = 'Computers' THEN
        -> SET S_Course = 'B.Tech(CSE)';
        -> ELSEIF Sub = 'History' THEN
        -> SET S_Course = 'Masters in History';
        -> ELSEIF Sub = 'Literature' THEN
        -> SET S_Course = 'Masters in English';
        -> END IF;
        -> END //
    Query OK, 0 rows affected (0.00 sec)
    8.

Chúng tôi sẽ sử dụng bảng

mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
4 từ cơ sở dữ liệu mẫu để trình diễn:

Hướng dẫn mysql procedure if else if - thủ tục mysql nếu khác nếu

Xem thủ tục lưu trữ

mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
5 sau đây.

DELIMITER $$ CREATE PROCEDURE GetCustomerLevel( IN pCustomerNumber INT, OUT pCustomerLevel VARCHAR(20)) BEGIN DECLARE credit DECIMAL(10,2) DEFAULT 0; SELECT creditLimit INTO credit FROM customers WHERE customerNumber = pCustomerNumber; IF credit > 50000 THEN SET pCustomerLevel = 'PLATINUM'; END IF; END$$ DELIMITER ;

Code language: SQL (Structured Query Language) (sql)

Quy trình được lưu trữ

mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
5 chấp nhận hai tham số:
mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
7 và
mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
8.

  • Đầu tiên, chọn
    mysql> Delimiter ; //
    
    mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> Select @S_Course;
    +-------------+
    | @S_Course   |
    +-------------+
    | B.Tech(CSE) |
    +-------------+
    1 row in set (0.00 sec)
    
    mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> Select @S_Course;
    +--------------------+
    | @S_Course          |
    +--------------------+
    | Masters in English |
    +--------------------+
    1 row in set (0.00 sec)
    9 của khách hàng được chỉ định bởi
    mysql> Delimiter ; //
    
    mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> Select @S_Course;
    +-------------+
    | @S_Course   |
    +-------------+
    | B.Tech(CSE) |
    +-------------+
    1 row in set (0.00 sec)
    
    mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> Select @S_Course;
    +--------------------+
    | @S_Course          |
    +--------------------+
    | Masters in English |
    +--------------------+
    1 row in set (0.00 sec)
    7 từ bảng
    mysql> Delimiter ; //
    
    mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> Select @S_Course;
    +-------------+
    | @S_Course   |
    +-------------+
    | B.Tech(CSE) |
    +-------------+
    1 row in set (0.00 sec)
    
    mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> Select @S_Course;
    +--------------------+
    | @S_Course          |
    +--------------------+
    | Masters in English |
    +--------------------+
    1 row in set (0.00 sec)
    4 và lưu trữ nó trong biến cục bộ

    IF condition THEN statements; END IF;

    Code language: SQL (Structured Query Language) (sql)
    2.
  • Sau đó, đặt giá trị cho tham số

    IF condition THEN statements; END IF;

    Code language: SQL (Structured Query Language) (sql)
    3
    mysql> Delimiter ; //
    
    mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> Select @S_Course;
    +-------------+
    | @S_Course   |
    +-------------+
    | B.Tech(CSE) |
    +-------------+
    1 row in set (0.00 sec)
    
    mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> Select @S_Course;
    +--------------------+
    | @S_Course          |
    +--------------------+
    | Masters in English |
    +--------------------+
    1 row in set (0.00 sec)
    8 thành

    IF condition THEN statements; END IF;

    Code language: SQL (Structured Query Language) (sql)
    5 nếu giới hạn tín dụng của khách hàng lớn hơn

    IF condition THEN statements; END IF;

    Code language: SQL (Structured Query Language) (sql)
    6.

Tuyên bố này tìm thấy tất cả các khách hàng có giới hạn tín dụng lớn hơn

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
6:

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)

Đây là đầu ra một phần:

Hướng dẫn mysql procedure if else if - thủ tục mysql nếu khác nếu

Các câu lệnh này gọi quy trình được lưu trữ

mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
5 cho khách hàng 141 và hiển thị giá trị của tham số

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
3
mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
8:

CALL GetCustomerLevel(141, @level); SELECT @level;

Code language: SQL (Structured Query Language) (sql)
Vì khách hàng 141 có giới hạn tín dụng lớn hơn

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
6, mức của nó được đặt thành

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
5 như mong đợi.
Hướng dẫn mysql procedure if else if - thủ tục mysql nếu khác nếu

Because the customer 141 has a credit limit greater than

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
6, its level is set to

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
5 as expected.

Tuyên bố MySQL mysql> DELIMITER // ; mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))     -> BEGIN     -> DECLARE Sub Varchar(20);     -> SELECT Subject INTO SUB     -> FROM Student_info WHERE S_Subject = Subject;     -> IF Sub = 'Computers' THEN     -> SET S_Course = 'B.Tech(CSE)';     -> ELSEIF Sub = 'History' THEN     -> SET S_Course = 'Masters in History';     -> ELSEIF Sub = 'Literature' THEN     -> SET S_Course = 'Masters in English';     -> END IF;     -> END // Query OK, 0 rows affected (0.00 sec)0

Trong trường hợp bạn muốn thực hiện các câu lệnh khác khi

mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
7 trong nhánh
mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
7 không đánh giá thành
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
8, bạn có thể sử dụng câu lệnh
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
0 như sau:

IF condition THEN statements; ELSE else-statements; END IF;

Code language: SQL (Structured Query Language) (sql)

Trong cú pháp này, nếu

mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
7 đánh giá thành
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
8,

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
0 giữa
mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
9 và

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
2 thực thi. Mặt khác,

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
3 giữa thực thi

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
2 và
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
6.

Hãy để sửa đổi quy trình được lưu trữ

mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
5.

Đầu tiên, hãy bỏ thủ tục lưu trữ

mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
5:

DROP PROCEDURE GetCustomerLevel;

Code language: SQL (Structured Query Language) (sql)

Sau đó, tạo quy trình được lưu trữ

mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
5 với mã mới:

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
0

Trong quy trình được lưu trữ mới này, chúng tôi bao gồm chi nhánh

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
2. Nếu

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
2 không lớn hơn

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
6, chúng tôi sẽ đặt cấp độ khách hàng thành

CALL GetCustomerLevel(141, @level); SELECT @level;

Code language: SQL (Structured Query Language) (sql)
2 trong khối giữa

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
2 và
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
6.

Truy vấn này tìm thấy khách hàng có giới hạn tín dụng nhỏ hơn hoặc bằng

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
6:

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
1

Hình ảnh này cho thấy đầu ra một phần:

Hướng dẫn mysql procedure if else if - thủ tục mysql nếu khác nếu

Các báo cáo sau đây gọi quy trình được lưu trữ cho số khách hàng

CALL GetCustomerLevel(141, @level); SELECT @level;

Code language: SQL (Structured Query Language) (sql)
6 & nbsp; và hiển thị giá trị của tham số

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
3
mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
8:

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
2 Giới hạn tín dụng của khách hàng

CALL GetCustomerLevel(141, @level); SELECT @level;

Code language: SQL (Structured Query Language) (sql)
6 nhỏ hơn

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
6, do đó, tuyên bố trong chi nhánh

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
2 thực thi và đặt giá trị của tham số

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
3
mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
8 thành

CALL GetCustomerLevel(141, @level); SELECT @level;

Code language: SQL (Structured Query Language) (sql)
2.
Hướng dẫn mysql procedure if else if - thủ tục mysql nếu khác nếu

The credit limit of the customer

CALL GetCustomerLevel(141, @level); SELECT @level;

Code language: SQL (Structured Query Language) (sql)
6 is less than

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
6, therefore, the statement in the

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
2 branch executes and sets the value of the

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
3 parameter
mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
8 to

CALL GetCustomerLevel(141, @level); SELECT @level;

Code language: SQL (Structured Query Language) (sql)
2.

Tuyên bố MySQL IF condition THEN statements; ELSE else-statements; END IF;Code language: SQL (Structured Query Language) (sql)5

Nếu bạn muốn thực thi các câu lệnh một cách có điều kiện dựa trên nhiều điều kiện, bạn sẽ sử dụng câu lệnh

IF condition THEN statements; ELSE else-statements; END IF;

Code language: SQL (Structured Query Language) (sql)
5 sau:

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
3

Trong cú pháp này, nếu

mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
7 đánh giá thành
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
8, & nbsp;

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
0 trong chi nhánh
mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
9 thực thi; Nếu không,

DROP PROCEDURE GetCustomerLevel;

Code language: SQL (Structured Query Language) (sql)
1 tiếp theo được đánh giá.

Nếu

DROP PROCEDURE GetCustomerLevel;

Code language: SQL (Structured Query Language) (sql)
1 đánh giá thành
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
8,

DROP PROCEDURE GetCustomerLevel;

Code language: SQL (Structured Query Language) (sql)
4 thực thi; Nếu không,

DROP PROCEDURE GetCustomerLevel;

Code language: SQL (Structured Query Language) (sql)
1 tiếp theo được đánh giá.

Tuyên bố

IF condition THEN statements; ELSE else-statements; END IF;

Code language: SQL (Structured Query Language) (sql)
5 có thể có nhiều nhánh

DROP PROCEDURE GetCustomerLevel;

Code language: SQL (Structured Query Language) (sql)
7.

Nếu không có điều kiện nào trong

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
7 và

DROP PROCEDURE GetCustomerLevel;

Code language: SQL (Structured Query Language) (sql)
9 đánh giá thành
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
8,

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
3 trong nhánh

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
2 sẽ thực thi.

Chúng tôi sẽ sửa đổi quy trình được lưu trữ ____ 35 & nbsp; để sử dụng câu lệnh

IF condition THEN statements; ELSE else-statements; END IF;

Code language: SQL (Structured Query Language) (sql)
5.

Đầu tiên, hãy bỏ thủ tục lưu trữ

mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
5:

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
4

Sau đó, tạo quy trình được lưu trữ

mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
5 với mã mới:

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
5

Trong quy trình được lưu trữ mới này, chúng tôi bao gồm chi nhánh

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
2. Nếu

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
2 không lớn hơn

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
6, chúng tôi sẽ đặt cấp độ khách hàng thành

CALL GetCustomerLevel(141, @level); SELECT @level;

Code language: SQL (Structured Query Language) (sql)
2 trong khối giữa

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
2 và
mysql> DELIMITER // ;
mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
    -> BEGIN
    -> DECLARE Sub Varchar(20);
    -> SELECT Subject INTO SUB
    -> FROM Student_info WHERE S_Subject = Subject;
    -> IF Sub = 'Computers' THEN
    -> SET S_Course = 'B.Tech(CSE)';
    -> ELSEIF Sub = 'History' THEN
    -> SET S_Course = 'Masters in History';
    -> ELSEIF Sub = 'Literature' THEN
    -> SET S_Course = 'Masters in English';
    -> END IF;
    -> END //
Query OK, 0 rows affected (0.00 sec)
6.

  • Truy vấn này tìm thấy khách hàng có giới hạn tín dụng nhỏ hơn hoặc bằng

    IF condition THEN statements; END IF;

    Code language: SQL (Structured Query Language) (sql)
    6:
  • Hình ảnh này cho thấy đầu ra một phần:
  • Các báo cáo sau đây gọi quy trình được lưu trữ cho số khách hàng

    CALL GetCustomerLevel(141, @level); SELECT @level;

    Code language: SQL (Structured Query Language) (sql)
    6 & nbsp; và hiển thị giá trị của tham số

    IF condition THEN statements; END IF;

    Code language: SQL (Structured Query Language) (sql)
    3
    mysql> Delimiter ; //
    
    mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> Select @S_Course;
    +-------------+
    | @S_Course   |
    +-------------+
    | B.Tech(CSE) |
    +-------------+
    1 row in set (0.00 sec)
    
    mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> Select @S_Course;
    +--------------------+
    | @S_Course          |
    +--------------------+
    | Masters in English |
    +--------------------+
    1 row in set (0.00 sec)
    8:

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
2 Giới hạn tín dụng của khách hàng

CALL GetCustomerLevel(141, @level); SELECT @level;

Code language: SQL (Structured Query Language) (sql)
6 nhỏ hơn

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
6, do đó, tuyên bố trong chi nhánh

SELECT customerNumber, creditLimit FROM customers WHERE creditLimit > 50000 ORDER BY creditLimit DESC;

Code language: SQL (Structured Query Language) (sql)
2 thực thi và đặt giá trị của tham số

IF condition THEN statements; END IF;

Code language: SQL (Structured Query Language) (sql)
3
mysql> Delimiter ; //

mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)
8 thành

CALL GetCustomerLevel(141, @level); SELECT @level;

Code language: SQL (Structured Query Language) (sql)
2.

Tuyên bố MySQL

IF condition THEN statements; ELSE else-statements; END IF;

Code language: SQL (Structured Query Language) (sql)
5
Hướng dẫn mysql procedure if else if - thủ tục mysql nếu khác nếu

If you test the stored procedure with the customer that has a credit limit of 10000 or less, you will get the output as

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 125  | Raman   | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
13.

Nếu bạn muốn thực thi các câu lệnh một cách có điều kiện dựa trên nhiều điều kiện, bạn sẽ sử dụng câu lệnh

IF condition THEN statements; ELSE else-statements; END IF;

Code language: SQL (Structured Query Language) (sql)
5 sau:

Hướng dẫn này có hữu ích không?

Chúng ta có thể sử dụng nếu khác trong thủ tục lưu trữ?

Câu lệnh IF khác kiểm soát luồng thực thi trong SQL Server.Nó có thể được sử dụng trong các thủ tục được lưu trữ, chức năng, kích hoạt, v.v. để thực hiện các câu lệnh SQL dựa trên các điều kiện được chỉ định.Boolean_expression: một biểu thức boolean trả về đúng hoặc sai.It can be used in stored-procedures, functions, triggers, etc. to execute the SQL statements based on the specified conditions. Boolean_expression: A boolean expression that returns TRUE or FALSE.

Tôi có thể sử dụng nếu khác trong mysql không?

MySQL IF-THEN-RETSE Tuyên bố để thực thi các câu lệnh khi một điều kiện trong khối IF không đánh giá là TRUE, bạn có thể sử dụng if-then-else.you can use IF-THEN-ELSE .

Làm thế nào thêm nếu MySQL khác?

Cú pháp cho câu lệnh if-then-else trong mysql là: if stature1 thì {... các câu lệnh thực thi khi điều kiện1 là đúng ...} [otherif stature2 thì {...IF condition1 THEN {... statements to execute when condition1 is TRUE...} [ ELSEIF condition2 THEN {...

Chúng ta có thể viết thủ tục trong mysql không?

Một thủ tục là một chương trình con (như một chương trình con) trong một ngôn ngữ kịch bản thông thường, được lưu trữ trong cơ sở dữ liệu.Trong trường hợp của MySQL, các quy trình được viết trong MySQL và được lưu trữ trong cơ sở dữ liệu/máy chủ của MySQL.Quy trình MySQL có tên, danh sách tham số và (các) câu lệnh SQL.procedures are written in MySQL and stored in the MySQL database/server. A MySQL procedure has a name, a parameter list, and SQL statement(s).