您当前的位置: 首页 > 数据库教程 > MySQL教程 > mysql数据库条件判断查询语句的常见写法说明

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

作者:不详 来源:网络 发布时间: 2014-07-27 21:04 点击:
大家都知道只有的了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

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

  大家都知道只有的了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 < 10 do

  ->   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<=10 DO

  ->    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)
分享到:
本文"mysql数据库条件判断查询语句的常见写法说明"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4