成都创新互联网站制作重庆分公司

mysql怎么取随机整数,mysql 随机取值

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

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

成都创新互联公司主要从事成都做网站、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务于田,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

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

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

LIMIT 1

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;

请教一下,如何在mysql中插入一条指定位数的随机的整数,而且不可以重复

随机不能保证不重复,银行卡号不是随机生成,

随机取过值后要自己判断是否重复,重复了再取,再判断,

如果要取的值的位数都一样的话,告诉你个简单的方法,

比如取3位随机整数mt_rand(100,999);

不用循环着一位一位的取

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

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

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

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

怎么从mysql表中随机取数据

从mysql数据库表中随机获取数据

其实,从数据库随机获取数据,很简单,只须用到一个rand()函数;

select * from table_name order by rand() limit 0,5;

下面是一个小实例:

从文章表中随机获取5条数据。

$dblink=mysql_connect("localhost","root","123456");

mysql_query("set names utf8");

mysql_select_db("aixuexi");

$sql="select * from waxx_article order by rand() limit 0,5";

$rs=mysql_query($sql);

while($row=mysql_fetch_array($rs)){

$rows[]=$row;

}

if($rows){

foreach($rows as $v){

?

div style="width:350px;height:35px;line-height:35px;border:1px solid #ccc;"?php echo $v['title']; ?/div

?php

}

}else{

echo "暂无文章";

}

mysql怎么生成不重复的随机数

MySql数据库随机函授Rand()可以产生0~1之间带小数的随机数,只要将括号中的参数留空,所产生的数是完全随机的,出现重复的几率不大,但是不可避免会有重复的可能,如果需要完全避免重复,可以先用随机函数产生一堆数字,然后用剔除其中的重复数这个办法来实现。

例如先用循环产生一批随机数并存入数据表中,再用

select distinct colName from tableName;

选出无重复的随机数予以应用


网站栏目:mysql怎么取随机整数,mysql 随机取值
文章转载:http://cxhlcq.cn/article/hsjiic.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部