mysql的rand函數(shù)可以生成一個0到1之間的隨機(jī)數(shù),進(jìn)行一定的放大即可得到一個隨機(jī)數(shù)。再通過條件查詢來限制新隨機(jī)數(shù)沒有在表中出現(xiàn)過。如下所示:
創(chuàng)新互聯(lián)建站是專業(yè)的承德縣網(wǎng)站建設(shè)公司,承德縣接單;提供做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行承德縣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
SELECT FLOOR(RAND() * 99999) AS random_numFROM numbers
WHERE "random_num" NOT IN (SELECT my_number FROM numbers)
LIMIT 1
MYSQL 取隨機(jī)數(shù)
2010年04月26日 星期一 09:48
mysql 取隨機(jī)數(shù)
--對一個表取任意隨機(jī)數(shù)
SELECT *
FROM TMP_XF_TEST
WHERE ID = (SELECT FLOOR(RAND() * (SELECT MAX(ID) FROM TMP_XF_TEST)))
order by id LIMIT 1;
--有條件性的取隨機(jī)數(shù)
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 | 玄風(fēng) | 9 | 123 | 2010-04-26 14:56:39 | 2010-04-26 14:56:39 | 玄風(fēng)測試使用的數(shù)據(jù) |
+-----+-----------+-----+---------+---------------------+---------------------+--------------------+
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 | 玄風(fēng) | 9 | 123 | 2010-04-28 15:47:19 | 2010-04-28 15:47:19 | 玄風(fēng)測試使用的數(shù)據(jù) | 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 | 玄風(fēng) | 9 | 123 | 2010-04-28 15:47:19 | 2010-04-28 15:47:19 | 玄風(fēng)測試使用的數(shù)據(jù) |
+------+-----------+-----+---------+---------------------+---------------------+--------------------+
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)
對應(yīng)的另外一種杯具寫法是:
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;
rand() 隨機(jī)生成 0 - 1的浮點數(shù) , 常與其他函數(shù)結(jié)合使用 ,比如?ceiling,floor,LPAD 等
如果要指定指定范圍的隨機(jī)整數(shù)的話,需要用這個公式FLOOR(i?+?RAND()?*?j),比如?
# 生成 7 - 11的隨機(jī)數(shù)? SELECT FLOOR(7 + (RAND() * 5));
floor 地板; 取小于該值的最大整數(shù) ,比如 0
mysql select floor(1.23),floor(-1.23);
1? ? ? ? ? ?-2?
ceiling 則相反,向上取整,取大于該值的最小整數(shù) ,比如
SELECT CEILING(1.23); # 2
SELECT CEIL(-1.23); # -1
lpad 是左填充, 用法如下 :
LPAD(RAND()*31 + 1,2,'0')) # 取01-31的隨機(jī)整數(shù) ,保留兩位,如果是一位,左邊填0
MySql數(shù)據(jù)庫隨機(jī)函授Rand()可以產(chǎn)生0~1之間帶小數(shù)的隨機(jī)數(shù),只要將括號中的參數(shù)留空,所產(chǎn)生的數(shù)是完全隨機(jī)的,出現(xiàn)重復(fù)的幾率不大,但是不可避免會有重復(fù)的可能,如果需要完全避免重復(fù),可以先用隨機(jī)函數(shù)產(chǎn)生一堆數(shù)字,然后用剔除其中的重復(fù)數(shù)這個辦法來實現(xiàn)。
例如先用循環(huán)產(chǎn)生一批隨機(jī)數(shù)并存入數(shù)據(jù)表中,再用
select distinct colName from tableName;
選出無重復(fù)的隨機(jī)數(shù)予以應(yīng)用
在mysql調(diào)用rand函數(shù)生成隨機(jī)數(shù),sql代碼示例: UPDATE `表名` SET `字段名`=ceiling(rand()*500000+500000) WHERE (條件);