Skip to main content
Version: 2.8.x(Latest)

Introduction

Commonly used data query methods:

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)

Brief explanation:

  1. All is used to query and return a list/array of multiple records.
  2. One is used to query and return a single record.
  3. Array is used to query data of specified field columns and return an array.
  4. Value is used to query and return one field value, often used in conjunction with the Fields method.
  5. Count is used to query and return the number of records.

Additionally, it can be seen that these four methods' definitions also support direct input of conditional parameters, with parameter types consistent with the Where method. However, it is important to note that the Array and Value methods require at least a field parameter to be input.

Usage Examples

// 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()