What are some alternative approaches to using RAND() in MySQL to achieve random selection of data without encountering caching issues?

When using RAND() in MySQL to achieve random selection of data, caching issues can occur because the result of RAND() is cached for the duration of the query. One alternative approach is to use ORDER BY RAND() with a unique identifier column to ensure a different random order each time the query is executed. This prevents caching issues and provides a truly random selection of data.

$query = "SELECT * FROM table_name ORDER BY RAND(id)";
$result = mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($result)) {
    // Process each row as needed
}