+-
使用GORM在MySQL中获取NULL日期时间值
我想使用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)
最佳答案
Go特别不识别NULL.
我认为你可以使用GORM中的原始查询来实现它.
像这样.

db.Raw("SELECT * FROM visit_details WHERE out_time is NULL order by id desc limit 1").Scan(&visitDetail)

这是获取列的一个.希望这可以帮助.

点击查看更多相关文章

转载注明原文:使用GORM在MySQL中获取NULL日期时间值 - 乐贴网