我正在尝试使用其中一个高级随机函数从mysql数据库中获取一行。但是,当我尝试过滤以仅获取其中包含“field = 1”的行时,一切都会失败。有人可以发现我的错误吗?
SELECT username, message, ttime
FROM table WHERE done LIKE '1' AS r1 JOIN
(SELECT (RAND() *
(SELECT MAX(id)
FROM database)) AS id)
AS r2
WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 1;";
答案 0 :(得分:1)
您的问题中的查询有多个语法错误。 你想要这样的东西吗?
SELECT username, message, ttime
FROM database.table1 as r1
INNER JOIN
(SELECT (RAND() * MAX(id)) as a_random_id
FROM database.table1) r2
ON (r1.id >= r2.a_random_id)
WHERE done = '1'
ORDER BY r1.id ASC
LIMIT 1
我想知道这种变体是否更快:
SELECT r.username, r.message, r.ttime
FROM database.table1 r
WHERE r.id = (
SELECT MIN(r1.id) <<-- this should make MySQL stop after the first hit.
FROM database.table1 r1
INNER JOIN
(SELECT (RAND() * MAX(id)) as a_random_id
FROM database.table1) AS r2
ON (r1.id >= r2.a_random_id)
WHERE r1.done = '1') sub
哦,你没有从database
中选择,你从数据库中的 table
中选择。