You are here

public function Select::orderRandom in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/Database/Query/Select.php \Drupal\Core\Database\Query\Select::orderRandom()
  2. 9 core/lib/Drupal/Core/Database/Driver/pgsql/Select.php \Drupal\Core\Database\Driver\pgsql\Select::orderRandom()
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Query/Select.php \Drupal\Core\Database\Query\Select::orderRandom()

Orders the result set by a random value.

This may be stacked with other orderBy() calls. If so, the query will order by each specified field, including this one, in the order called. Although this method may be called multiple times on the same query, doing so is not particularly useful.

Note: The method used by most drivers may not scale to very large result sets. If you need to work with extremely large data sets, you may create your own database driver by subclassing off of an existing driver and implementing your own randomization mechanism. See

http://jan.kneschke.de/projects/mysql/order-by-rand/

for an example of such an alternate sorting mechanism.

Return value

$this The called object

Overrides SelectInterface::orderRandom

1 method overrides Select::orderRandom()
Select::orderRandom in core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
Orders the result set by a random value.

File

core/lib/Drupal/Core/Database/Query/Select.php, line 665

Class

Select
Query builder for SELECT statements.

Namespace

Drupal\Core\Database\Query

Code

public function orderRandom() {
  $alias = $this
    ->addExpression('RAND()', 'random_field');
  $this
    ->orderBy($alias);
  return $this;
}