为了账号安全,请及时绑定邮箱和手机立即绑定

不符合在 golang 中使用 gorm 的条件

不符合在 golang 中使用 gorm 的条件

Go
哆啦的时光机 2022-11-23 20:01:14
我有两个数据库 Booking 和 Room。Booking 将 roomid 作为其字段之一。我写了一个 select 语句,它将检索到的行保存在结果变量中,如下所述。var result models.Bookingrows, err := utils.DB.Model(&currRequest).Where("check_in BETWEEN ? AND ? AND check_out BETWEEN ? AND ?", currRequest.CheckIn, currRequest.CheckOut, currRequest.CheckIn, currRequest.CheckOut).Select("room_id").Rows()for rows.Next() {            utils.DB.ScanRows(rows, &result)            fmt.Println(result.RoomID)        }现在我的 result.roomid 具有满足 bookings 表中的 select 语句的 roomids 值我的结果变量可能有多个房间 ID 值。我能够通过遍历结果变量来检索 roomid 值。现在我必须检查名为 Room 的主房间数据库,并获取那些不在结果结构中的房间 ID。通过使用以下语句,我只能访问 result.roomid 中的第一个值,因此 not in 条件仅考虑 result.roomid 中的第一个值。我如何对 result.roomid 中的所有值执行不符合条件?rows, err := utils.DB.Model(&models.Room{}).Not(result.RoomID).Select("room_id").Rows()完整代码:package handlersimport (    "encoding/json"    "fmt"    "net/http"    "server/models"    "server/utils"    "strings")func AvailableRoomsHandler(w http.ResponseWriter, r *http.Request) {    currRequest := &models.Booking{}    err := json.NewDecoder(r.Body).Decode(currRequest)    //check if a valid request has been sent from front end    if err != nil {        //fmt.Println(err)        var resp = map[string]interface{}{"status": false, "message": "Invalid json request"}        json.NewEncoder(w).Encode(resp)        return    }当我执行 result.roomid 时,它只给出第一个房间 ID,并在上面的 select 语句中只删除那个房间 ID。如何消除我在房间表数据中的预订表中找到的所有房间 ID?我尝试拆分 result.roomid 值并尝试形成一个字符串并在 select 语句中给出它,但这没有用。我尝试遍历每个 result.roomid 并在语句中运行 not 但这没有任何意义。
查看完整描述

1 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

使用此代码:


var result models.Booking

rows, err := utils.DB.Model(&currRequest).Where("check_in BETWEEN ? AND ? AND check_out BETWEEN ? AND ?", currRequest.CheckIn, currRequest.CheckOut, currRequest.CheckIn, currRequest.CheckOut).Select("room_id").Rows()

if err != nil {

        json.NewEncoder(w).Encode(err)

        fmt.Print("error occured in select statement")

        return

} else {

        defer rows.Close()

        for rows.Next() {

            noOfRoomsOccupied = noOfRoomsOccupied + 1

            utils.DB.ScanRows(rows, &result)

            //rest of the code

        }

}

您只能从结果集中获得一行,其中可能有很多行。要获取所有行并提取它们的值,您应该使用[]models.Booking.


result := []models.Booking{}

rows, err := utils.DB.Model(&currRequest).Where("check_in BETWEEN ? AND ? AND check_out BETWEEN ? AND ?", currRequest.CheckIn, currRequest.CheckOut, currRequest.CheckIn, currRequest.CheckOut).Select("room_id").Rows()

if err != nil {

        json.NewEncoder(w).Encode(err)

        fmt.Print("error occured in select statement")

        return

} else {

        defer rows.Close()

        for rows.Next() {

            var b models.Booking

            noOfRoomsOccupied = noOfRoomsOccupied + 1

            utils.DB.ScanRows(rows, &b)

            result = append(result, b)

            //rest of the code

        }

}

但是,由于无论如何你只需要,你可以通过使用(假设is 类型)roomid使它更容易。[]uintroomiduint


result := []uint{}

rows, err := utils.DB.Model(&currRequest).Where("check_in BETWEEN ? AND ? AND check_out BETWEEN ? AND ?", currRequest.CheckIn, currRequest.CheckOut, currRequest.CheckIn, currRequest.CheckOut).Select("room_id").Rows()

if err != nil {

        json.NewEncoder(w).Encode(err)

        fmt.Print("error occured in select statement")

        return

} else {

        defer rows.Close()

        for rows.Next() {

            var rid uint

            noOfRoomsOccupied = noOfRoomsOccupied + 1

            utils.DB.ScanRows(rows, &rid)

            result = append(result, rid)

            //rest of the code

        }

}

随着resulttype 的存在[]uint,将它与Not函数一起使用会更容易(根据文档):


rows, err := utils.DB.Model(&models.Room{}).Not(result).Select("room_id").Rows()


查看完整回答
反对 回复 2022-11-23
  • 1 回答
  • 0 关注
  • 86 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信