Last active
December 15, 2015 18:39
-
-
Save netojoaobatista/5305640 to your computer and use it in GitHub Desktop.
Obtendo registros aleatórios de uma base sem utilizar ORDER BY RAND()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pdo = new PDO('mysql:host=127.0.0.1;dbname=base', 'user', 'pswd'); | |
$stm = $pdo->query(' | |
SET @count = (SELECT COUNT(*) FROM tabela); | |
SET @query = CONCAT("SELECT * FROM `tabela` LIMIT 4 OFFSET ", 1 + FLOOR(RAND() * @count)); | |
PREPARE stmt FROM @query; | |
EXECUTE stmt; | |
'); | |
$stm->nextRowset(); //SET @count... | |
$stm->nextRowset(); //SET @query... | |
$stm->nextRowset(); //PREPARE... | |
foreach ($stm->fetchAll(PDO::FETCH_OBJ) as $row) { //EXECUTE... | |
var_dump($row->name); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#obtendo o total de registros da tabela | |
SET @count = (SELECT COUNT(*) FROM `tabela`); | |
#criando a consulta para obter 4 registros sequenciais, iniciando de um ponto aleatório | |
SET @query = CONCAT("SELECT * FROM `tabela` LIMIT 4 OFFSET ", 1 + FLOOR(RAND() * @count)); | |
#preparado a consulta | |
PREPARE stmt FROM @query; | |
#executando | |
EXECUTE stmt; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pdo = new PDO('mysql:host=127.0.0.1;dbname=base', 'user', 'pswd'); | |
$stm = $pdo->query(' | |
SET @count = (SELECT COUNT(*) FROM tabela); | |
SET @query = CONCAT(" | |
SELECT * | |
FROM (SELECT * FROM `tabela` LIMIT 1 OFFSET ", 1 + FLOOR(RAND() * @count), ") AS `t1` | |
UNION SELECT * | |
FROM (SELECT * FROM `tabela` LIMIT 1 OFFSET ", 1 + FLOOR(RAND() * @count), ") AS `t2` | |
UNION SELECT * | |
FROM (SELECT * FROM `tabela` LIMIT 1 OFFSET ", 1 + FLOOR(RAND() * @count), ") AS `t3` | |
UNION SELECT * | |
FROM (SELECT * FROM `tabela` LIMIT 1 OFFSET ", 1 + FLOOR(RAND() * @count), ") AS `t4`"); | |
PREPARE stmt FROM @query; | |
EXECUTE stmt; | |
'); | |
$stm->nextRowset(); //SET @count... | |
$stm->nextRowset(); //SET @query... | |
$stm->nextRowset(); //PREPARE... | |
foreach ($stm->fetchAll(PDO::FETCH_OBJ) as $row) { //EXECUTE... | |
var_dump($row->name); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#obtendo o total de registros da tabela | |
SET @count = (SELECT COUNT(*) FROM `tabela`); | |
#criando a consulta para obter 4 registros aleatórios | |
SET @query = CONCAT(" | |
SELECT * | |
FROM (SELECT * FROM `tabela` LIMIT 1 OFFSET ", 1 + FLOOR(RAND() * @count), ") AS `t1` | |
UNION SELECT * | |
FROM (SELECT * FROM `tabela` LIMIT 1 OFFSET ", 1 + FLOOR(RAND() * @count), ") AS `t2` | |
UNION SELECT * | |
FROM (SELECT * FROM `tabela` LIMIT 1 OFFSET ", 1 + FLOOR(RAND() * @count), ") AS `t3` | |
UNION SELECT * | |
FROM (SELECT * FROM `tabela` LIMIT 1 OFFSET ", 1 + FLOOR(RAND() * @count), ") AS `t4`"); | |
#preparado a consulta | |
PREPARE stmt FROM @query; | |
#executando | |
EXECUTE stmt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment