你需要用两个查询,而不是一做。
获取的符合条件的记录数
$stmt212count = $db->prepare("
SELECT
count(*)
FROM
websites w
INNER JOIN
users u ON
u.username = w.owner
AND u.coins >= :coins
");
$stmt212count->bindValue('coins', 1, PDO::PARAM_INT);
$stmt212count->execute();
$row212count = $stmt212count->fetch(PDO::FETCH_COLUMN);
选择一个随机行
# random row offset
$offset = rand(0, $row212count-1);
使用这种说法,如果你有PDO仿真准备打开
$stmt212 = $db->prepare(sprintf(
"
SELECT
*
FROM
websites w
INNER JOIN
users u ON
u.username = w.owner
AND u.coins >= :coins
LIMIT %d,1
",
$offset
);
$stmt212count->bindValue('offset', $offset, PDO::PARAM_INT);
使用这个,如果你不使用PDO仿真准备
$stmt212 = $db->prepare("
SELECT
*
FROM
websites w
INNER JOIN
users u ON
u.username = w.owner
AND u.coins >= :coins
LIMIT :offset,1
");
时使用此两个语句
$stmt212count->bindValue('coins', 1, PDO::PARAM_INT);
$stmt212count->execute();
$row212 = $stmt212->fetch();
是u.username ,w.owner和u.coin ■所有索引 – nogad
http://jan.kneschke.de/projects/mysql/order-by-rand/ –
^笑甚至没有看到的是,为了通过兰特表现死亡http://stackoverflow.com/questions/ 1823306/MySQL的替代品,按订单按兰特 – nogad