第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在 GORM 中迭代一個 int 數(shù)組

如何在 GORM 中迭代一個 int 數(shù)組

Go
富國滬深 2023-01-03 17:16:31
在Params模型中我有一個 int 數(shù)組Cat_id我提出一個要求:localhost:8080/products/?cat_id=1,2 我想展示這兩個類別的多個產(chǎn)品。我如何解析地構(gòu)建我的查詢?我的功能: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}當(dāng)我使用時Debug,我得到了這個: SELECT * FROM "products" WHERE cat_id=(1,2) AND "products"."deleted_at" IS NULL
查看完整描述

1 回答

?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗 獲得超4個贊

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

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

  2. Where將您的條款更改為如下內(nèi)容:

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



查看完整回答
反對 回復(fù) 2023-01-03
  • 1 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號