我试图为我在1到60之间选择的每一行生成一个随机整数作为计时器。
SELECT downloads.date, products.*, (FLOOR(1 + RAND() * 60)) AS timer
我已经搜索并继续使用此FLOOR函数作为如何选择范围内的随机整数。这给了我每一行1。 我错过了什么?
我在mysql 5.0.75
继续查询的其余部分我相信它可能是一个嵌套问题
SELECT *
FROM (
SELECT downloads.date, products.*, FLOOR(1 + (RAND() * 60)) AS randomtimer,
(
SELECT COUNT( * )
FROM distros
WHERE distros.product_id = products.product_id
) AS distro_count,
(SELECT COUNT(*) FROM downloads WHERE downloads.product_id = products.product_id) AS true_downloads
FROM downloads
INNER JOIN products ON downloads.product_id = downloads.product_id
) AS count_table
WHERE count_table.distro_count > 0
AND count_table.active = 1
ORDER BY count_table.randomtimer , count_table.date DESC LIMIT 10
答案 0 :(得分:90)
这对我有用。你的mysql版本可能吗?
SELECT id, (FLOOR( 1 + RAND( ) *60 )) AS timer
FROM users
LIMIT 0 , 30
答案 1 :(得分:2)
我正在运行您的查询,它确实为每行提供了一个随机数....也许与随机(计时器)的名称有关?
答案 2 :(得分:1)
RAND
函数的输出将始终为 0 和 1 之间的值。
试试这个:
SELECT downloads.date, products.*, (CAST(RAND() * 60 AS UNSIGNED) + 1) AS timer
答案 3 :(得分:1)
老问题,但总是实际问题。
这里有一种创建 MySQL 函数的方法 random_integer()
based on manual :
CREATE FUNCTION random_integer(value_minimum INT, value_maximum INT)
RETURNS INT
COMMENT 'Gets a random integer between value_minimum and value_maximum, bounds included'
RETURN FLOOR(value_minimum + RAND() * (value_maximum - value_minimum + 1));
SELECT ALL random_integer(1, 60) AS timer;
答案 4 :(得分:0)
您可以将数字乘以表格中的记录数量。
SELECT id,
(FLOOR( (SELECT MIN(id) FROM your_table ) + RAND( ) * 1000000 ) ) AS timer
FROM your_table
LIMIT 0 , 30