如何获得 Gorm 中特定日期的最大值?

发布于 01-20 09:45 字数 807 浏览 3 评论 0原文

我已经写了以下代码,以获取使用Gorm的每日最大值。

  1. 我经过当前的时间,开始一天的开始和结束。
  2. 我在一天的开始和结束之间选择所有值。
  3. 我订购温度并获得第一个。

我的代码:

func GetDailyMaxTemperature(ts time.Time) (*Temperature, error) {
    temp:= &Temperature{}
    start, end := getStartAndEndOfDay(ts)
    if tx := db.Where("ts BETWEEN ? AND ?", start, end).Order("temperature ASC").First(temp); tx.Error != nil {
        return temp, tx.Error
    }
    return temp, nil

func getStartAndEndOfDay(ts time.Time) (time.Time, time.Time) {
    dayStart := time.Date(ts.Year(), ts.Month(), ts.Day(), 0, 0, 0, 0, ts.Location())
    dayEnd := time.Date(ts.Year(), ts.Month(), ts.Day(), 23, 59, 59, 999, ts.Location())
    return dayStart, dayEnd
}

此代码有效,但是我对此并不满意,并且想知道是否有更多的“ Gorm-ish”方法获得特定表的最大值,尤其是在涉及日期时。

I have written following code to get the daily maximum of a certain value with GORM.

  1. I pass the current time and get the day's start and end.
  2. I select all values between the day's start and end.
  3. I order the temperatures and get the first.

My Code:

func GetDailyMaxTemperature(ts time.Time) (*Temperature, error) {
    temp:= &Temperature{}
    start, end := getStartAndEndOfDay(ts)
    if tx := db.Where("ts BETWEEN ? AND ?", start, end).Order("temperature ASC").First(temp); tx.Error != nil {
        return temp, tx.Error
    }
    return temp, nil

func getStartAndEndOfDay(ts time.Time) (time.Time, time.Time) {
    dayStart := time.Date(ts.Year(), ts.Month(), ts.Day(), 0, 0, 0, 0, ts.Location())
    dayEnd := time.Date(ts.Year(), ts.Month(), ts.Day(), 23, 59, 59, 999, ts.Location())
    return dayStart, dayEnd
}

This code works, however I am not very satisfied with it and wonder if there are more "GORM-ish" ways to get a maximum value of a certain table especially when there are dates involved.

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

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

发布评论

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

评论(1

怎樣才叫好2025-01-27 09:45:31

您可以在SQL中使用max操作。也许不是一种更“ gorm”的方法,但我认为在语义上更正确/有吸引力的版本:

var result float64
row := db.Table("temperatures").Where("ts BETWEEN ? AND ?", start, end).Select("max(temperature)").Row()
err := row.Scan(&result)

您可以将其作为这样的温度结构的方法:

func (t Temperature) MaxInRange(db *gorm.DB, start, end time.Time) (float64, err) {
  var result float64
  row := db.Table("temperatures").Where("ts BETWEEN ? AND ?", start, end).Select("max(temperature)").Row()
  err := row.Scan(&result)
  return result, err
}

You could use the max operation in SQL. Maybe not a more 'GORM' way to do this, but in my opinion a more semantically correct/appealing version:

var result float64
row := db.Table("temperatures").Where("ts BETWEEN ? AND ?", start, end).Select("max(temperature)").Row()
err := row.Scan(&result)

You could make this a method of the Temperature struct like this:

func (t Temperature) MaxInRange(db *gorm.DB, start, end time.Time) (float64, err) {
  var result float64
  row := db.Table("temperatures").Where("ts BETWEEN ? AND ?", start, end).Select("max(temperature)").Row()
  err := row.Scan(&result)
  return result, err
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文