如何在drupal视图中添加DISTINCT、GROUP BY子句
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Group By 不适用于 hook_views_query_alter,这被称为 drupal 视图模块问题。报告于https://drupal.org/node/590534,http://drupal.org/node/1608742, http://drupal.org/node/389230#comment-2637200
另外,聚合函数不是视图给出所需的结果,因为它将添加所有字段并排序顺序字段以按条件分组。
因此,您应该使用其他挂钩,例如 hook_query_alter。
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.
在视图编辑表单的基本设置下,尝试打开独特设置。
您还可以尝试在基本设置下的样式部分使用齿轮图标。如果您的行样式是字段,您可以在此处指定要分组的字段。
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.
在 Drupal 8 中,您将这样添加“分组依据”:
In Drupal 8, you will add "Group by" that way: