简体   繁体   English

使用 Gorm 查询多列

[英]Querying for multiple columns with Gorm

My db includes the following columns, "model_package" and "model_variant".我的数据库包括以下列“model_package”和“model_variant”。 I try to query the db with Gorm by specifying both of these columns inside.Select(), however, I keep getting a scan error.我尝试通过在.Select() 中指定这两个列来使用 Gorm 查询数据库,但是,我不断收到扫描错误。 Normally, when I select a single column (ie .Select("model_package") ), it returns to an array(slice) containing all the values, so I assumed it should return to a two-dimension array(slice) when I select multiple columns like in my code below.通常,当我 select 单列(即.Select("model_package") )时,它返回一个包含所有值的数组(切片),所以我假设它应该返回一个二维数组(切片)当我 select多列,如我下面的代码中。

My goal is to combine both fields into a string.我的目标是将两个字段组合成一个字符串。 For example, if a row in db has the following values "model_package": "pkg1" and "model_variant": "var1", then I want to create this string "pkg1_var1".例如,如果db中的一行具有以下值“model_package”:“pkg1”和“model_variant”:“var1”,那么我想创建这个字符串“pkg1_var1”。 How can I construct the correct query to get both values for each row on the db.如何构造正确的查询以获取数据库上每一行的两个值。

My code:我的代码:

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
}

Error:错误:

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

A couple of options you can try out:您可以尝试几个选项:

Option 1 - create a view model for your SELECT query and return that to do with it what you want.选项 1 - 为您的 SELECT 查询创建一个视图 model 并返回它来处理您想要的。

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
}

Option 2 - do the concatenation with the CONCAT() function within your SELECT query.选项 2 - 在 SELECT 查询中使用CONCAT() function 进行连接。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM