使用Gorm查询多列数据

huangapple go评论85阅读模式
英文:

Querying for multiple columns with Gorm

问题

你的代码中出现了扫描错误,错误信息显示目标不是一个指针。这个错误是因为你传递给.Scan()方法的参数类型不正确。.Scan()方法需要传递指向目标变量的指针,而你传递的是一个二维字符串切片。你可以尝试修改代码,将modelNames声明为一个结构体切片,然后使用结构体的字段来存储查询结果。

修改后的代码如下所示:

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
}

这样修改后,查询结果将会存储在modelNames切片中,每个元素都是一个ModelName结构体,包含model_packagemodel_variant字段的值。

英文:

My db includes the following columns, "model_package" and "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. Normally, when I select a single column (i.e. .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.

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". 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

答案1

得分: 2

你可以尝试以下几个选项:

选项1 - 为你的SELECT查询创建一个视图模型,并返回它以便进行后续操作。

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
}

选项2 - 在你的SELECT查询中使用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
}
英文:

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.

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.

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
}

huangapple
  • 本文由 发表于 2021年6月16日 00:47:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/67990394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定