gpt4 book ai didi

mysql - 使用 GORM 在 MySQL 中获取 NULL 日期时间值

转载 作者:IT王子 更新时间:2023-10-29 00:57:40 25 4
gpt4 key购买 nike

我想使用 Gorm 获取 out_time 为 NULL 的最后一个 visit_details 行。NIL 本身是一种类型,其中 VisitDetail OutTime 是 mysql.NullTime

代码:-

var visitDetail models.VisitDetail
db.Where("out_time=? ", nil).Last(&visitDetail)

//model VisitDetails
type VisitDetail struct {
Id int
Visitor Visitor `gorm:"foreignkey:ClientId;association_foreignkey:Id"`
VisitorId int `gorm:"not null;"`
Location Location `gorm:"foreignkey:LocationId;association_foreignkey:Id"`
LocationId int `gorm:"not null;"`
Purpose string
InTime time.Time `gorm:"not null;"`
OutTime mysql.NullTime
User User `gorm:"foreignkey:ClientId;association_foreignkey:Id"`
UserId int `gorm:"not null;"`
Status int `gorm:"not null;"`
ApproveByClient int `gorm:"not null;"`
}

查询:-

select * from visit_details where out_time is NULL order by id desc limit 1;
+----+------------+-------------+---------+---------------------+----------+---------+--------+-------------------+
| id | visitor_id | location_id | purpose | in_time | out_time | user_id | status | approve_by_client |
+----+------------+-------------+---------+---------------------+----------+---------+--------+-------------------+
| 20 | 1 | 8 | | 2018-02-20 17:13:25 | NULL | 1609 | 0 | 0 |
+----+------------+-------------+---------+---------------------+----------+---------+--------+-------------------+
1 row in set (0.04 sec)

最佳答案

您可以将 OutTime 设置为一个指针,这样它就可以为空。正如在 doc 中看到的那样:

type YourStruct struct {
OutTime *time.Time
}

然后运行你的查询

db.Where("out_time = ?", nil).Last(&visitDetail)

关于mysql - 使用 GORM 在 MySQL 中获取 NULL 日期时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48899014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com