I have the following MySQL. I want to pull the data in random order.
Could anyonet teach me how to do it please.
$Q = $this->db->query('SELECT P.*, C.Name AS CatName
FROM products AS P
LEFT JOIN categories C
ON C.id = P.category_id
WHERE C.Name = "Front bottom"
AND p.status = "active"
');
you can use the
RANDfunction of MySQL to do that, to be noted that it would perform really slowly on huge dataset (~ about 10k). MySQL would pickup a random number for each row of the table which could lead to problem if the table is huge.A safer method would be to do a
SELECT count(*) as n FROM tableand to pickup a random number and do a query withLIMIT 1,nto pickup the nth row. That would work if you need only 1, or you don’t care having the result in same order.After if you really need a complete random set better to do it on server side in my opinion.