logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

Scroll
12/04/2025, by Ivan

Some SQL queries may return duplicate results. In such cases, duplicate rows can be filtered using the “DISTINCT” keyword in a static query. In a dynamic query, use the Different() method.

// Force filtering of duplicate records in the result set.
$connection = \Drupal::database();
$query = $connection->select('my_table', 'mt');
$query->fields('mt', ['my_fields']);
$query->distinct()->execute()->fetchAll();

Note that DISTINCT can lead to reduced performance, so do not use it unless there is no other way to limit the result set to avoid duplication.