如何使用视图在 drupal 中获取 Min() 或 Max() ?

发布于 2024-08-23 15:46:47 字数 255 浏览 5 评论 0原文

我正在使用 Drupal 的 Views 2,并且需要从自定义表中的字段中检索最小值。如果我用手写的话,这个查询很容易——比如“SELECT foo, min(bar) FROM table GROUP BY foo”。但我该如何使用视图来做到这一点呢?我已经在views.info 文件中定义了该表,因此让视图查看该表没有任何问题。这是我不明白的查询的 Min() 部分。

我的下一站将是 Views API 文档,但如果有人能够提供如何快速完成此操作的大纲,我将不胜感激。

I'm using Drupal's Views 2, and need to retrieve min values from fields in a custom table. The query for this is easy if I were writing it by hand--something like, "SELECT foo, min(bar) FROM table GROUP BY foo." But how would I do this using Views? I've already defined the table in a views.info file, so there's no trouble getting views to see the table. It's the Min() part of the query I just don't understand.

My next stop will be the Views API documentation, but if someone can just provide the outline for how to do this quickly, I would greatly appreciate it.

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

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

发布评论

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

评论(3

花伊自在美 2024-08-30 15:46:47

老问题的新答案,但这样的东西会起作用。您需要创建一个自定义字段处理程序,然后按如下方式包装该字段:

class views_handler_custom_field extends views_handler_field {

  function query() {
    $this->ensure_my_table();
    $this->field_alias = $this->query->add_field("MAX({$this->table_alias}", "{$this->real_field})",$this->table_alias . "_" . $this->real_field);
  }
}

New answer to an old question, but something like this will work. You need to create a custom field handler and then wrap the field as follows:

class views_handler_custom_field extends views_handler_field {

  function query() {
    $this->ensure_my_table();
    $this->field_alias = $this->query->add_field("MAX({$this->table_alias}", "{$this->real_field})",$this->table_alias . "_" . $this->real_field);
  }
}
苏璃陌 2024-08-30 15:46:47
  1. 使用视图高级配置中的聚合。设置完这个之后
    是的,您可以为字段选择最大值、最小值或任何其他选择器。

测试您的结果,但它应该运行良好

  1. 或者在某些情况下您可以对数据进行升序或排序
    降序,然后选择一个要在视图上显示的选项。可以是
    显示多个字段时出现问题。

经过第一个测试后,至少在小规模上似乎更快。

  1. Use aggregation from advanced configuration of views. After this is set
    yes you can select max, min or any other selector to fields.

Test your results but it should work well

  1. Alternatively in some cases you can sort your data ascending or
    descending and then just pick one to be shown on the view. Can be
    problematic when displaying multiple fields or so.

After testing first one seems to be faster at least on small scale.

夏花。依旧 2024-08-30 15:46:47

人们可以考虑模块 groupbyviews_calc,但我认为它们对您来说是不可接受的。

此外,您可以使用自定义模块来完成此操作。

One could consider the modules groupby and views_calc, but I assume they are not acceptable for you.

Additionally, you can accomplish this with a custom module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文