在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
),你可以做這兩件事:
將
pagination.Cat_id
字符串轉(zhuǎn)換為[]int64
切片(我們稱此變量catIDs
為 type[]int64
)以獲取具有分隔int64
元素的切片。Where
將您的條款更改為如下內(nèi)容:result := config.DB.Model(&models.Products{}).Where(q).Where("cat_id IN (?)", catIDs).Limit(pagination.Limit).Offset(offset).Find(&prod)
- 1 回答
- 0 關(guān)注
- 192 瀏覽
添加回答
舉報
0/150
提交
取消