Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

All/One/Array/Value/Count

这五个方法是数据查询比较常用的方法,方法列表:

Code Block
languagego
func (m *Model) All(where ...interface{} (Result, error)
func (m *Model) One(where ...interface{}) (Record, error)
func (m *Model) Array(fieldsAndWhere ...interface{}) ([]Value, error)
func (m *Model) Value(fieldsAndWhere ...interface{}) (Value, error)
func (m *Model) Count(where ...interface{}) (int, error)
func (m *Model) CountColumn(column string) (int, error)

简要说明:

  1. All  用于查询并返回多条记录的列表/数组。
  2. One  用于查询并返回单条记录。
  3. Array  用于查询指定字段列的数据,返回数组。
  4. Value  用于查询并返回一个字段值,往往需要结合Fields方法使用。
  5. Count  用于查询并返回记录数。

此外,也可以看得到这四个方法定义中也支持条件参数的直接输入,参数类型与Where方法一致。但需要注意,其中ArrayValue方法的参数中至少应该输入字段参数。

使用示例:

Code Block
languagego
// SELECT * FROM `user` WHERE `score`>60
Model("user").Where("score>?", 60).All()

// SELECT * FROM `user` WHERE `score`>60 LIMIT 1
Model("user").Where("score>?", 60).One()

// SELECT `name` FROM `user` WHERE `score`>60
Model("user").Fields("name").Where("score>?", 60).Array()

// SELECT `name` FROM `user` WHERE `uid`=1 LIMIT 1
Model("user").Fields("name").Where("uid", 1).Value()

// SELECT COUNT(1) FROM `user` WHERE `status` IN(1,2,3)
Model("user").Where("status", g.Slice{1,2,3}).Count()

Find*支持主键条件的数据查询

Info
title提示

新版已不支持此方法

方法列表:

AllAndCount

该方法用于同时查询数据记录列表及总数量,一般用于分页查询场景中。方法定义如下:

Code Block
languagego
func (m *Model) FindAll(where ...interface{}) (Result, error)
func (m *Model) FindOne(where ...interface{}) (Record, error)
func (m *Model) FindArray(fieldsAndWhere ...interface{}) (Value, error)
func (m *Model) FindValue(fieldsAndWhere ...interface{}) (Value, error)
func (m *Model) FindCount(where ...interface{}) (int, error// AllAndCount retrieves all records and the total count of records from the model.
// If useFieldForCount is true, it will use the fields specified in the model for counting;
// otherwise, it will use a constant value of 1 for counting.
// It returns the result as a slice of records, the total count of records, and an error if any.
// The where parameter is an optional list of conditions to use when retrieving records.
//
// Example:
//
//	var model Model
//	var result Result
//	var count int
//	where := []interface{}{"name = ?", "John"}
//	result, count, err := model.AllAndCount(true)
//	if err != nil {
//	    // Handle error.
//	}
//	fmt.Println(result, count)
func (m *Model) AllAndCount(useFieldForCount bool) FindScan(pointerresult interface{}Result, where ...interface{})totalCount int, err error)

在方法内部查询总数量时,将会忽略查询中的Limit/Page操作。Find*方法包含:FindAll/FindOne/FineValue/FindCount/FindScan,这些方法与All/One/Array/Value/Count/Scan方法的区别在于,当方法直接给定条件参数时,前者的效果与WherePri方法一致;而后者的效果与Where方法一致。也就是说Find*方法的条件参数支持智能主键识别特性。

使用示例:

Code Block
languagego
// SELECT *`uid`,`name` FROM `scores``user` WHERE `id`=1
Model("scores").FindAll(1)

`status`='deleted' LIMIT 0,10
// SELECT *COUNT(`uid`,`name`) FROM `scores``user` WHERE `id`=1 LIMIT 1
`status`='deleted'
all, count, err := Model("scoresuser").FindOne(1)

// SELECT `name` FROM `scores` WHERE `id`=1
Model("scoresFields("uid", "name").FindArrayWhere("status", "namedeleted").Limit(0, 110).AllAndCount(true)

// SELECT `uid`,`name` FROM `scores``user` WHERE `id``status`=1'deleted' LIMIT 1
Model("user").FindValue("name", 1)
0,10
// SELECT COUNT(1) FROM `user`  WHERE `id`=1 
WHERE `status`='deleted'
all, count, err := Model("user").FindCount(1)
关于Scan/FindScan方法介绍,请查看后续章节。
Fields("uid", "name").Where("status", "deleted").Limit(0, 10).AllAndCount(false)







Panel
titleContent Menu

Table of Contents