Heim > Datenbank > MySQL-Tutorial > Hauptteil

mysql数据库条件判断查询语句的常见写法说明

WBOY
Freigeben: 2016-06-07 16:16:25
Original
1944 Leute haben es durchsucht

大家都知道只有的了mysql 5后才出现了存储过程,这些用法,那么我们要在sql 中用if else while这些,就得用存过程或函数来实例了。 mysql DELIMITER // mysql CREATE FUNCTION myFunction (quantity INT(10)) RETURNS INT(10) - BEGIN - - WHILE quantity MO

大家都知道只有的了mysql 5后才出现了存储过程,,这些用法,那么我们要在sql 中用if else while这些,就得用存过程或函数来实例了。

mysql> DELIMITER //
mysql> CREATE FUNCTION myFunction (quantity INT(10)) RETURNS INT(10)
    -> BEGIN
    ->
    ->     WHILE quantity MOD 12 > 0 DO
    ->     SET quantity = quantity + 1;
    ->     END WHILE;
    ->
    ->     RETURN quantity;
    ->
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql>
mysql> select myFunction(10);
+----------------+
| myFunction(10) |
+----------------+
|             12 |
+----------------+
1 row in set (0.00 sec)

mysql>
mysql> select myFunction(24);
+----------------+
| myFunction(24) |
+----------------+
|             24 |
+----------------+
1 row in set (0.00 sec)

实例二

mysql> delimiter //
mysql> create procedure test_while (IN in_count INT)
    -> BEGIN
    ->     declare count INT default 0;
    ->
    ->     while count     ->         set count = count + 1;
    ->     end while;
    ->
    ->     select count;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql> call test_while(10);
+-------+
| count |
+-------+
|    10 |
+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

带有条件判断的

mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->
    ->     DECLARE i int;
    ->     SET i=1;
    ->     loop1: WHILE i     ->          IF MOD(i,2)0 THEN /*Even number - try again*/
    ->             SELECT CONCAT(i," is an odd number");
    ->          END IF;
    ->          SET i=i+1;
    ->     END WHILE loop1;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql> call myProc();
+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 1 is an odd number            |
+-------------------------------+
1 row in set (0.02 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 3 is an odd number            |
+-------------------------------+
1 row in set (0.02 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 5 is an odd number            |
+-------------------------------+
1 row in set (0.02 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 7 is an odd number            |
+-------------------------------+
1 row in set (0.02 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 9 is an odd number            |
+-------------------------------+
1 row in set (0.02 sec)

Query OK, 0 rows affected (0.38 sec)

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!