Loopback 4-如何在列中找到最高值(例如表格中的最大值(列))?

发布于 2025-01-25 18:35:23 字数 208 浏览 3 评论 0原文

我想在特定表的特定列中找到最高值。它应该非常简单。 这是lb4 https://loopback.io/doc/doc/en/lb2/在哪里进行滤波器,但我在那里没有找到它。

I want to find the highest value in a specific column of a specific table. It should be very simple.
this is the documentation of LB4 https://loopback.io/doc/en/lb2/Where-filter But I didn't find it there.

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

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

发布评论

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

评论(2

冷︶言冷语的世界 2025-02-01 18:35:23

我们通过一种自定义存储库方法来执行此操作,在该方法中,我们执行一个select max()查询,并具有调用它的自定义控制器方法(IE /Next-ID)。

存储库方法:

  async nextId(): Promise<any> {
    return this.dataSource
      .execute('select MAX(id)+5 as nextId from route_lookup')
      .then(data => {
        if (data[0].NEXTID === null) {
          data[0].NEXTID = 1005;
        }
        return data[0].NEXTID;
      });
  }

控制器方法:

  @get('/route-lookups/next-id')
  @response(200, {
    description: 'Next avaialble id for route lookup',
    content: {
      'application/json': {
        schema: {
          type: 'number',
        },
      },
    },
  })
  async nextId(): Promise<number> {
    return await this.routeLookupRepository.nextId();
  }

We did this through a custom repository method where we execute a select max() query and have a custom controller method (i.e. /next-id) that calls it.

Repository method:

  async nextId(): Promise<any> {
    return this.dataSource
      .execute('select MAX(id)+5 as nextId from route_lookup')
      .then(data => {
        if (data[0].NEXTID === null) {
          data[0].NEXTID = 1005;
        }
        return data[0].NEXTID;
      });
  }

Controller method:

  @get('/route-lookups/next-id')
  @response(200, {
    description: 'Next avaialble id for route lookup',
    content: {
      'application/json': {
        schema: {
          type: 'number',
        },
      },
    },
  })
  async nextId(): Promise<number> {
    return await this.routeLookupRepository.nextId();
  }
oО清风挽发oО 2025-02-01 18:35:23

loopback filter文档即使不是那么明显的方法,即使它并不那么明显。

/weapons?filter[where][effectiveRange][gt]=900&filter[limit]=3

本质上,您可以执行以下操作:

  1. 确定感兴趣的列。
  2. 请使用gt运算符设置最小号码
  3. 如果您想确保按预期确保排序顺序,
  4. 添加订单。将结果限制为1。

这是一个代码示例:

Employees.find({
  where: {
    age: {
      gt: 1
    }
  },
  order: 'age ASC',
  limit: 1
})

请让我知道这是否是您想要的,或者是否需要更多支持。

Within the Loopback Filter Documentation they do mention a way to achieve this, even though it's not as obvious.

/weapons?filter[where][effectiveRange][gt]=900&filter[limit]=3

Essentially you can do the following:

  1. Identify the column of interest.
  2. Use the gt operator to set a min number
  3. Add order if you wanted to ensure the sorting order is as expected.
  4. Limit the results to 1.

Here is a code example:

Employees.find({
  where: {
    age: {
      gt: 1
    }
  },
  order: 'age ASC',
  limit: 1
})

Please let me know if this is what you were going for or if you need some more support.

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