Introduction

Commonly used data query methods:

  1. func (m *Model) All(where ...interface{} (Result, error)
  2. func (m *Model) One(where ...interface{}) (Record, error)
  3. func (m *Model) Array(fieldsAndWhere ...interface{}) ([]Value, error)
  4. func (m *Model) Value(fieldsAndWhere ...interface{}) (Value, error)
  5. func (m *Model) Count(where ...interface{}) (int, error)
  6. 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

  1. // SELECT * FROM `user` WHERE `score`>60
  2. Model("user").Where("score>?", 60).All()
  3. // SELECT * FROM `user` WHERE `score`>60 LIMIT 1
  4. Model("user").Where("score>?", 60).One()
  5. // SELECT `name` FROM `user` WHERE `score`>60
  6. Model("user").Fields("name").Where("score>?", 60).Array()
  7. // SELECT `name` FROM `user` WHERE `uid`=1 LIMIT 1
  8. Model("user").Fields("name").Where("uid", 1).Value()
  9. // SELECT COUNT(1) FROM `user` WHERE `status` IN(1,2,3)
  10. Model("user").Where("status", g.Slice{1,2,3}).Count()