亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 GORM 中迭代一個 int 數組

如何在 GORM 中迭代一個 int 數組

Go
富國滬深 2023-01-03 17:16:31
在Params模型中我有一個 int 數組Cat_id我提出一個要求:localhost:8080/products/?cat_id=1,2 我想展示這兩個類別的多個產品。我如何解析地構建我的查詢?我的功能:func GetAllIproducts(q *models.Products, pagination *models.Params) (*[]models.Products, error) {    var prod []models.Products    offset := (pagination.Page - 1) * pagination.Limit    result := config.DB.Model(&models.Products{}).Where(q).Where("cat_id=?", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod) //Problem is here    if result.Error != nil {        msg := result.Error        return nil, msg    }    return &prod, nil}當我使用時Debug,我得到了這個: SELECT * FROM "products" WHERE cat_id=(1,2) AND "products"."deleted_at" IS NULL
查看完整描述

1 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

假設cat_id是一個整數(假設int64),你可以做這兩件事:

  1. pagination.Cat_id字符串轉換為[]int64切片(我們稱此變量catIDs為 type []int64)以獲取具有分隔int64元素的切片。

  2. Where將您的條款更改為如下內容:

     result := config.DB.Model(&models.Products{}).Where(q).Where("cat_id IN (?)", catIDs).Limit(pagination.Limit).Offset(offset).Find(&prod)



查看完整回答
反對 回復 2023-01-03
  • 1 回答
  • 0 關注
  • 236 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號