mysql怎么输出随机数 mysql随机取数

在mysql 数据库如何添加随机数字

在mysql中随机生成一些范围内的整数,可以借助mysql提供的二个函数来实现。

双清网站建设公司创新互联,双清网站设计制作,有大型网站制作公司丰富经验。已为双清1000多家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的双清做网站的公司定做!

它们是floor()与rand()函数,下面分别介绍下。

1、floor(f)

返回一个不大于f的最大整数

2、rand(),rand(n)

返回一个随机浮点值 v ,范围在 0 到1 之间 (即, 其范围为 0 ≤ v ≤ 1.0)。若已指定一个整数参数 N ,则它被用作种子值,用来产生重复序列。

若要在i ≤ R ≤ j 这个范围得到一个随机整数R ,需要用到表达式 FLOOR(i + RAND() * (j – i + 1))。

例如, 在7到 12 的范围(包括7和12)内得到一个随机整数,使用语句:

SELECT FLOOR(7 + (RAND() * 6));

MySQL产生随机数

MYSQL 取随机数

2010年04月26日 星期一 09:48

mysql 取随机数

--对一个表取任意随机数

SELECT *

FROM TMP_XF_TEST

WHERE ID = (SELECT FLOOR(RAND() * (SELECT MAX(ID) FROM TMP_XF_TEST)))

order by id LIMIT 1;

--有条件性的取随机数

SELECT *

FROM TMP_XF_TEST

WHERE ID = (SELECT FLOOR(RAND() *

((SELECT MAX(ID) FROM TMP_XF_TEST WHERE GID = 9) -

(SELECT MIN(ID) FROM TMP_XF_TEST WHERE GID = 9))) +

(SELECT MIN(ID) FROM TMP_XF_TEST WHERE GID = 9))

AND GID = 9

ORDER BY ID LIMIT 1;

--gid上存在索引

或者

SELECT *

FROM TMP_XF_TEST AS t1 JOIN

(SELECT ROUND(RAND() * ((SELECT MAX(id) FROM TMP_XF_TEST WHERE GID = 9)-(SELECT MIN(id) FROM TMP_XF_TEST WHERE GID = 9))

+(SELECT MIN(id) FROM TMP_XF_TEST WHERE GID = 9)) AS id) AS t2

WHERE t1.id = t2.id AND t1.GID = 9

ORDER BY t1.id LIMIT 1;

#########

不要用下面的杯具写法

mysql insert into tmp_xf_test(user_nick,gid,item_id,gmt_create,gmt_modified,memo)

- select user_nick,gid,item_id,gmt_create,gmt_modified,memo from tmp_xf_test;

Query OK, 165888 rows affected (9.65 sec)

Records: 165888 Duplicates: 0 Warnings: 0

mysql SELECT *

- FROM `tmp_xf_test`

- WHERE id = (SELECT FLOOR( MAX(id) * RAND()) FROM `tmp_xf_test` )

- ORDER BY id LIMIT 1;

+-----+-----------+-----+---------+---------------------+---------------------+--------------------+

| id | user_nick | gid | item_id | gmt_create | gmt_modified | memo |

+-----+-----------+-----+---------+---------------------+---------------------+--------------------+

| 467 | 玄风 | 9 | 123 | 2010-04-26 14:56:39 | 2010-04-26 14:56:39 | 玄风测试使用的数据 |

+-----+-----------+-----+---------+---------------------+---------------------+--------------------+

1 row in set (51.12 sec)

mysql explain SELECT *

- FROM `tmp_xf_test`

- WHERE id = (SELECT FLOOR( MAX(id) * RAND()) FROM `tmp_xf_test` )

- ORDER BY id LIMIT 1\G

*************************** 1. row ***************************

id: 1

select_type: PRIMARY

table: tmp_xf_test

type: index

possible_keys: NULL

key: PRIMARY

key_len: 8

ref: NULL

rows: 1

Extra: Using where

*************************** 2. row ***************************

id: 2

select_type: UNCACHEABLE SUBQUERY

table: tmp_xf_test

type: index

possible_keys: NULL

key: idx_tmp_xf_test_gid

key_len: 4

ref: NULL

rows: 331954

Extra: Using index

2 rows in set (0.01 sec)

---

mysql SELECT * FROM `tmp_xf_test` t1 join

- (SELECT FLOOR( MAX(id) * RAND()) as id FROM `tmp_xf_test` ) as t2

- where t1.id =t2.id

- ORDER BY t1.id LIMIT 1;

+-------+-----------+-----+---------+---------------------+---------------------+--------------------+-------+

| id | user_nick | gid | item_id | gmt_create | gmt_modified | memo | id |

+-------+-----------+-----+---------+---------------------+---------------------+--------------------+-------+

| 40311 | 玄风 | 9 | 123 | 2010-04-28 15:47:19 | 2010-04-28 15:47:19 | 玄风测试使用的数据 | 40311 |

+-------+-----------+-----+---------+---------------------+---------------------+--------------------+-------+

1 row in set (0.14 sec)

##############

mysql SELECT * FROM `tmp_xf_test`

- WHERE id = (SELECT floor(RAND() * (SELECT MAX(id) FROM `tmp_xf_test`)))

- ORDER BY id LIMIT 1;

+------+-----------+-----+---------+---------------------+---------------------+--------------------+

| id | user_nick | gid | item_id | gmt_create | gmt_modified | memo |

+------+-----------+-----+---------+---------------------+---------------------+--------------------+

| 1352 | 玄风 | 9 | 123 | 2010-04-28 15:47:19 | 2010-04-28 15:47:19 | 玄风测试使用的数据 |

+------+-----------+-----+---------+---------------------+---------------------+--------------------+

1 row in set (0.00 sec)

mysql explain SELECT * FROM `tmp_xf_test`

- WHERE id = (SELECT floor(RAND() * (SELECT MAX(id) FROM `tmp_xf_test`)))

- ORDER BY id LIMIT 1\G

*************************** 1. row ***************************

id: 1

select_type: PRIMARY

table: tmp_xf_test

type: index

possible_keys: NULL

key: PRIMARY

key_len: 8

ref: NULL

rows: 1

Extra: Using where

*************************** 2. row ***************************

id: 3

select_type: SUBQUERY

table: NULL

type: NULL

possible_keys: NULL

key: NULL

key_len: NULL

ref: NULL

rows: NULL

Extra: Select tables optimized away

2 rows in set, 1 warning (0.00 sec)

对应的另外一种杯具写法是:

SELECT *

FROM TMP_XF_TEST

WHERE ID = (SELECT FLOOR(RAND() * (MAX(ID) - MIN(ID))) + MIN(ID) MID

FROM TMP_XF_TEST

WHERE GID = 9)

AND GID = 9 LIMIT 1;

如何通过java向mysql输入一个随机数猜大小

1、首先通过控制台一直输入一个整数,比较该数与系统随机生成的那个数。

2、其次如果大就输出“猜大了。”,继续输入。如果小就输出“猜小了。”。

3、最后继续输入;如果相等就输出“恭喜,猜对了。”,退出输出。

mysql如何生成不重复的随机数

mysql的rand函数可以生成一个0到1之间的随机数,进行一定的放大即可得到一个随机数。再通过条件查询来限制新随机数没有在表中出现过。如下所示:

SELECT FLOOR(RAND() * 99999) AS random_numFROM numbers

WHERE "random_num" NOT IN (SELECT my_number FROM numbers)

LIMIT 1

MYSQL 获取随机数的问题!!!!!!!!!!!!!!!

给你提供一个思路你看是否可行;

1.随机数不用MYSQL生成,用程序(如PHP)生成,范围在ID的最大和最小之间;

2.根据生成的随机数去数据库直接查询,如果没有就重复一次操作;


分享标题:mysql怎么输出随机数 mysql随机取数
文章地址:http://hbruida.cn/article/hhpseg.html