我想获取最后一个 Visit_details 行,其 out_time 为NULL
使用戈尔姆。NIL
本身是一种类型,其中 VisitDetail OutTime 是mysql.NullTime
.
Code:-
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;"`
}
Query:-
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 http://gorm.io/docs/conventions.html#gorm-Model:
type YourStruct struct {
OutTime *time.Time
}
然后运行您的查询
db.Where("out_time = ?", nil).Last(&visitDetail)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)