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:
All
is used to query and return a list/array of multiple records.One
is used to query and return a single record.Array
is used to query data of specified field columns and return an array.Value
is used to query and return one field value, often used in conjunction with theFields
method.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()