mysql 定义function rand
MySQL获取随机数
如何通过MySQL在某个数据区间获取随机数?
MySQL本身提供一个叫rand的函数,返回的v范围为0 <= v < 1.0。
介绍此函数的MySQL文档也介绍道,可以通过此计算公式FLOOR(i + RAND() * (j – i)),获取i <= v < j的随机数字v。
附文档链接:http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand
为了书写、调用方便,写一个函数返回随机数字,注意,此函数为了遵应个人习惯,是使用FLOOR(i + RAND() * (j – i + 1))这样的公式,随机数v的范围为i <= v <= j,而非i <= v < j。
CREATE FUNCTION rand_num (
start_num INTEGER,
end_num INTEGER
) RETURNS INTEGER
BEGIN
RETURN FLOOR(start_num + RAND() * (end_num - start_num + 1));
END;
比如,要获取1-9的随机数,如此调用即可:
select rand_num(1, 9);
mysql 定义function rand的更多相关文章
- MYSQL随机抽取查询 MySQL Order By Rand()效率问题
MYSQL随机抽取查询:MySQL Order By Rand()效率问题一直是开发人员的常见问题,俺们不是DBA,没有那么牛B,所只能慢慢研究咯,最近由于项目问题,需要大概研究了一下MYSQL的随机 ...
- MySQL定义外键的方法
MySQL定义外键的方法是每个学习MySQL的人都需要掌握的知识,下文就对MySQL定义外键的语句写法进行了详细的阐述,供您参考. 外键为MySQL带来了诸多的好处,下面就为您介绍MySQL定义外键的 ...
- Error Code: 1630. FUNCTION rand.string does not exist
1.错误描述 13:50:13 call new_procedure Error Code: 1630. FUNCTION rand.string does not exist. Check the ...
- Mysql drop function xxxx ERROR 1305 (42000): FUNCTION (UDF) xxxx does not exist
mysql> drop function GetEmployeeInformationByID;ERROR 1305 (42000): FUNCTION (UDF) GetEmployeeInf ...
- mysql order by rand() 优化方法
mysql order by rand() 优化方法 适用于领取奖品等项目<pre>mysql> select * from user order by rand() limit 1 ...
- mysql yyyy-MM-dd function UNIX_TIMESTAMP('yyyy-MM-dd HH:mm:ss')
mysql yyyy-MM-dd function UNIX_TIMESTAMP('yyyy-MM-dd HH:mm:ss') select UNIX_TIMESTAMP('1997-10-04 22 ...
- MySQL This function has none of DETERMINISTIC, NO SQL...错误1418 的原因分析及解决方法
MySQL开启bin-log后,调用存储过程或者函数以及触发器时,会出现错误号为1418的错误: ERROR 1418 (HY000): This function has none of DETER ...
- MySQL定义数据库对象之指定definer
mysql创建view.trigger.function.procedure.event时都会定义一个Definer: SQL SECURITY 有两个选项,一个为DEFINER,一个为INVOKER ...
- Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)
catalog . How to Add New Functions to MySQL . Features of the User-Defined Function Interface . User ...
随机推荐
- 翻滚吧,Spark (错误记录)
1) 本地运行报错: Exception in thread "main" org.apache.spark.SparkException: A master URL must b ...
- xargs 命令
1. xargs 很好用,可以将多个结果分段传给下一个命令进行计算 比如说find 找到很多个文件,又想对每个文件统计条数: find 20151201 -name "mjoys*.txt& ...
- bzoj 1030 fail树dp
dp[i][j][0]代表当前匹配到i号点走了j步且没到过单词节点,1代表到过,直接转移. #include<iostream> #include<cstdio> #inclu ...
- 【caffe】基本数据结构blob
@tags: caffe blob blob是caffe中的基本数据结构,简单理解就是一个"4维数组".但是,这个4维数组有什么意义? BTW,TensorFlow这款google ...
- 一次性下载CVPR2016的所有文章
wget --no-clobber --convert-links --random-wait -r -p -E -e robots=off -U mozilla http://www.cv-foun ...
- bzoj2959: 长跑
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- codeforces 723D: Lakes in Berland
Description The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × ...
- Codeforces Round #389 Div.2 D. Santa Claus and a Palindrome
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 遵循PSR-4的自动加载
一.简介 首先这里要了解PSR,Proposing a Standards Recommendation(提出标准建议)的缩写,就是一种PHP开发规范,让我们研发出来的代码更合理.更好维护.可读性更高 ...
- android 事件传递机制 心得
看了网上很多资料. 最后我发现可以用很简单的几句话就能把它说清楚 1 每个 viewgroup 内都有 三个方法 a dispatchTouchEvent 是自己决定要不要(管他爹)要这个苹果的 一般 ...