logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

滚动
01/10/2025, by Ivan

某些 SQL 查询可能会返回重复的结果。在这种情况下,可以在静态查询中使用关键字「DISTINCT」来过滤重复的行。在动态查询中,请使用 distinct() 方法。

// 强制在结果集中过滤重复记录。
$connection = \Drupal::database();
$query = $connection->select('my_table', 'mt');
$query->fields('mt', ['my_fields']);
$query->distinct()->execute()->fetchAll();

请注意,DISTINCT 可能会导致性能下降,因此除非没有其他方式可以限制结果集以避免重复,否则不要使用它。