如何在drupal视图中添加DISTINCT、GROUP BY子句

发布于 2024-12-11 12:38:08 字数 466 浏览 0 评论 0原文

我正在使用 drupal 6,并且创建了一个视图,并且在视图查询中我想添加

GROUP BY AND DISTINCT CLAUSE。

为此,我尝试使用 views_groupby 贡献的模块,但它只提供 COUNT SQL 聚合函数。

我不知道如何使用hook_views_query_alter(&$view, &$query)在视图查询中添加group by子句和distinct子句。

所以它看起来像这样 -

SELECT DISTINCT(node.nid) AS nid, OTHER_COLUMN 
FROM TABLE_NAME 
JOIN ANOTHER_TABLE ON JOIN_CONDITION 
GROUP BY nid

任何帮助或建议将不胜感激。

I'm using drupal 6 and i've created a view and in view query i want to add

GROUP BY AND DISTINCT CLAUSE.

For that i tried to use views_groupby contributed module but it only provide COUNT SQL Aggregation Function.

I don't know how to add group by clause and distinct clause in view query using hook_views_query_alter(&$view, &$query).

So that it look like this -

SELECT DISTINCT(node.nid) AS nid, OTHER_COLUMN 
FROM TABLE_NAME 
JOIN ANOTHER_TABLE ON JOIN_CONDITION 
GROUP BY nid

Any help or suggestion would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一人独醉 2024-12-18 12:38:08

Group By 不适用于 hook_views_query_alter,这被称为 drupal 视图模块问题。报告于https://drupal.org/node/590534http://drupal.org/node/1608742, http://drupal.org/node/389230#comment-2637200

另外,聚合函数不是视图给出所需的结果,因为它将添加所有字段并排序顺序字段以按条件分组。
因此,您应该使用其他挂钩,例如 hook_query_alter

function hook_query_alter( &$query) {

  if($query->alterMetaData['view']->name == "your_views_name"){
     $query->groupBy("domain_source.domain_id");
     //also you can unset other group by critera by using, unset($query->getGroupBy());
  }
}

Group By is not working on hook_views_query_alter and it is known as drupal views module issue. which is reported at https://drupal.org/node/590534, http://drupal.org/node/1608742, http://drupal.org/node/389230#comment-2637200

Also views Aggregation Functions is not given desired result, because it will add all fields and sort order fields to group by criteria.
So, You should use other hooks such as hook_query_alter.

function hook_query_alter( &$query) {

  if($query->alterMetaData['view']->name == "your_views_name"){
     $query->groupBy("domain_source.domain_id");
     //also you can unset other group by critera by using, unset($query->getGroupBy());
  }
}
错爱 2024-12-18 12:38:08

在视图编辑表单的基本设置下,尝试打开独特设置。

您还可以尝试在基本设置下的样式部分使用齿轮图标。如果您的行样式是字段,您可以在此处指定要分组的字段。

On your View edit form, under Basic Settings, try turning the Distinct setting on.

You may also try to use the Gear Icon for the Style section under Basic Settings. There you can specify a field to group by, if your row style is fields.

假扮的天使 2024-12-18 12:38:08

在 Drupal 8 中,您将这样添加“分组依据”:

use Drupal\Core\Database\Query\AlterableInterface;

function MYMODULE_query_alter(AlterableInterface $query) {
  if (isset($query->alterMetaData['view'])) {
    if($query->alterMetaData['view']->id() == 'replace_by_view_machine_name') {
      // Group by UID to remove duplicates from View results
      $query->groupBy('users_field_data.uid');
    }
  }
}

In Drupal 8, you will add "Group by" that way:

use Drupal\Core\Database\Query\AlterableInterface;

function MYMODULE_query_alter(AlterableInterface $query) {
  if (isset($query->alterMetaData['view'])) {
    if($query->alterMetaData['view']->id() == 'replace_by_view_machine_name') {
      // Group by UID to remove duplicates from View results
      $query->groupBy('users_field_data.uid');
    }
  }
}
ゃ懵逼小萝莉 2024-12-18 12:38:08
function hook_views_query_alter(&$view, &$query) {
  if ($view->name == 'your_view_name') {
    $query->add_field('table', 'field', 'alias', array('function', groupby));
    $query->add_groupby('table.field');
  }
}
function hook_views_query_alter(&$view, &$query) {
  if ($view->name == 'your_view_name') {
    $query->add_field('table', 'field', 'alias', array('function', groupby));
    $query->add_groupby('table.field');
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文