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

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

使用 Gorm 查詢多個(gè)列

使用 Gorm 查詢多個(gè)列

Go
慕桂英546537 2022-09-05 17:35:25
我的數(shù)據(jù)庫包括以下列,“model_package”和“model_variant”。我嘗試通過在 里面指定這兩列來使用 Gorm 查詢 db。選擇(),但是,我不斷收到掃描錯(cuò)誤。通常,當(dāng)我選擇單個(gè)列(即)時(shí),它會(huì)返回到包含所有值的數(shù)組(切片),因此當(dāng)我選擇多列時(shí),我認(rèn)為它應(yīng)該返回到二維數(shù)組(切片),就像下面的代碼一樣。.Select("model_package")我的目標(biāo)是將這兩個(gè)字段組合成一個(gè)字符串。例如,如果db中的一行具有以下值“model_package”:“pkg1”和“model_variant”:“var1”,那么我想創(chuàng)建這個(gè)字符串“pkg1_var1”。如何構(gòu)造正確的查詢以獲取數(shù)據(jù)庫上每行的兩個(gè)值。我的代碼:func (s *store) ListAllModelNames() ([][]string, error) {    var modelNames [][]string    result := s.db.Table(mfcTable).Select("model_package", "model_variant").Scan(&modelNames)    if result.Error != nil {        return nil, result.Error    }    return modelNames, nil}錯(cuò)誤:sql: Scan error on column index 0, name "model_package": destination not a pointer; sql: Scan error on column index 0, name "model_package": destination not a pointer; sql: Scan error on column index 0, name "model_package": destination not a pointer; sql: Scan error on column index 0, name "model_package": destination not a pointer; sql: Scan error on column index 0, name "model_package": destination not a pointer; sql: Scan error on column index 0, name "model_package": destination not a pointer; sql: Scan error on column index 0, name "model_package": destination not a pointer; sql: Scan error on column index 0, name "model_package": destination not a pointer
查看完整描述

1 回答

?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以嘗試以下幾個(gè)選項(xiàng):


選項(xiàng) 1 - 為 SELECT 查詢創(chuàng)建一個(gè)視圖模型,并將其返回以執(zhí)行所需的操作。


type ModelName struct {

   ModelPackage string

   ModelVariant string

}


func (s *store) ListAllModelNames() ([]ModelName, error) {

    var modelNames []ModelName

    result := s.db.Table(mfcTable).Select("model_package", "model_variant").Scan(&modelNames)

    if result.Error != nil {

        return nil, result.Error

    }

    return modelNames, nil

}

選項(xiàng) 2 - 使用 SELECT 查詢中的函數(shù)進(jìn)行串聯(lián)。CONCAT()


func (s *store) ListAllModelNames() ([]string, error) {

    var modelNames []string

    result := s.db.Table(mfcTable).Select("CONCAT(model_package, "_", model_variant)").Where("model_package IS NOT NULL AND model_variant IS NOT NULL").Scan(&modelNames)

    if result.Error != nil {

        return nil, result.Error

    }

    return modelNames, nil

}


查看完整回答
反對 回復(fù) 2022-09-05
  • 1 回答
  • 0 關(guān)注
  • 341 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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