æ¦Âè¿°
GORMçÂÂç¹æ§
- å ¨åÂÂè½ ORM
- å ³è (Has Oneï¼ÂHas Manyï¼ÂBelongs Toï¼ÂMany To Manyï¼Âå¤ÂæÂÂï¼ÂÃ¥ÂÂ表继æ¿)
- Createï¼ÂSaveï¼ÂUpdateï¼ÂDeleteï¼ÂFind ä¸Âé©åÂÂæ¹æ³Â
- æ¯æÂÂÃÂ
Preload
ãÂÂJoins
àçÂÂé¢Âå 载 - äºÂå¡ï¼ÂåµÂå¥ÂäºÂå¡ï¼ÂSave Pointï¼ÂRollback To Saved Point
- ContextãÂÂé¢Âç¼Âè¯Â模å¼ÂãÂÂDryRun 模å¼Â
- æ¹éÂÂæÂÂå ¥ï¼ÂFindInBatchesï¼ÂFind/Create with Mapï¼Â使ç¨ SQL 表达å¼ÂãÂÂContext Valuer è¿Âè¡ CRUD
- SQL æÂÂ建å¨ï¼ÂUpsertï¼Âæ°æ®åºÂéÂÂï¼ÂOptimizer/Index/Comment Hintï¼Âå½åÂÂÃ¥ÂÂæ°ï¼ÂÃ¥ÂÂæ¥询
- å¤ÂÃ¥ÂÂ主é®ï¼Âç´¢å¼Âï¼Â约æÂÂ
- Auto Migration
- èªå®Âä¹ Logger
- çµ活çÂÂå¯æ©å±ÂæÂÂ件 APIï¼ÂDatabase Resolverï¼Âå¤Âæ°æ®åºÂï¼Â读åÂÂÃ¥ÂÂ离ï¼ÂãÂÂPrometheusâ¦
- æ¯Â个ç¹æ§é½ç»Âè¿ÂäºÂæµÂè¯ÂçÂÂéÂÂéÂÂèÂÂéªÂ
- å¼ÂÃ¥ÂÂè åÂÂ好
ä¸ÂãÂÂ模åÂÂå®Âä¹Â
æ¦Â念
模åÂÂæ¯æ ÂÃ¥ÂÂç struct
ï¼Âç± Go çÂÂåºæ¾°æ®类åÂÂãÂÂå®Âç°äºÂàScanner
Ã¥ÂÂàValuer
àæÂ¥å£çÂÂèªå®Âä¹Âç±»åÂÂÃ¥ÂÂÃ¥Â
¶æÂÂéÂÂæÂÂå«åÂÂç»ÂæÂÂãÂÂ
ä¾Âå¦Âï¼Â
type User struct {
ID uint
Name string
Email *string
Age uint8
Birthday *time.Time
MemberNumber sql.NullString
ActivatedAt sql.NullTime
CreatedAt time.Time
UpdatedAt time.Time
}
约å®Â
GORM å¾åÂÂäºÂ约å®Âä¼ÂäºÂéÂ
Âç½®ï¼Âé»Â认æÂÂ
åµä¸Âï¼ÂGORM 使ç¨àID
àä½Â为主é®ï¼Â使ç¨ç»ÂæÂÂä½ÂÃ¥ÂÂçÂÂàèÂÂå½¢å¤Âæ°
àä½Â为表åÂÂï¼ÂÃ¥ÂÂ段åÂÂçÂÂàèÂÂå½¢
àä½Â为åÂÂÃ¥ÂÂï¼Â并使ç¨àCreatedAt
ãÂÂUpdatedAt
àåÂÂ段追踪åÂÂ建ãÂÂæ´æ°æ¶é´ãÂÂ
gorm.Model
GORM å®Âä¹Âä¸Â个àgorm.Model
àç»ÂæÂÂä½Âï¼ÂÃ¥Â
¶åÂÂ
æÂŒÂÂ段àID
ãÂÂCreatedAt
ãÂÂUpdatedAt
ãÂÂDeletedAt
ï¼Âå¯以å°Âå®ÂåµÂÃ¥Â
¥å°èª己å®Âä¹ÂçÂÂç»ÂæÂÂä½Âä¸Âï¼Â以åÂÂ
å«è¿Âå 个åÂÂ段ãÂÂ
//gorm.Model çÂÂå®Âä¹Â
type Model struct{
ID uint `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
åµÂå ¥ç»ÂæÂÂä½Â
对äºÂå¿åÂÂÃ¥ÂÂ段ï¼ÂGORM ä¼Âå°Âå ¶åÂÂ段å å«å¨ç¶ç»ÂæÂÂä½Âä¸Âï¼Âä¾Âå¦Âï¼Â
type User struct {
gorm.Model
UserName string
Password string
}
// çÂÂæÂÂäºÂ
type User struct {
ID uint `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
UserName string
Password string
}
äºÂãÂÂè¿ÂæÂ¥å°æ°æ®åºÂ
ï¼Âæ¬人使ç¨çÂÂæ¯Mysqlï¼Âå°±ä¸Â说æÂÂå ¶ä»Âæ°æ®åºÂçÂÂè¿ÂæÂ¥æ¹æ³ÂäºÂï¼Â
Mysql
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
dsn := "user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db , err := gorm.Open(mysql.Open(dsn),&gorm.Config{})
}
ä¸ÂãÂÂCRUDæÂÂä½Â
1. Ã¥ÂÂ建记å½Â
Ã¥ÂÂ建ä¸Âæ¡æ°æ®
使ç¨db.Create
æ¹æ³Âï¼Âä¼ åÂ
¥ç»ÂæÂÂä½ÂçÂÂæÂÂéÂÂè¿Âè¡Âè®°å½ÂçÂÂÃ¥ÂÂ建ãÂÂ
user := User{UserName: "lps", Password: "123"}
result := db.Create(&user)
è¿ÂÃ¥ÂÂçÂÂæ°æ®ï¼Âuser.ID
(主é®ï¼Â,result.Error
(éÂÂ误),result.RowsAffected
(è¡Âæ°)
çÂÂÃ¥ÂÂäºÂ以ä¸Âsqlè¯Âå¥ãÂÂ
INSERT INTO
`users `
(`created_at`,`updated_at`,`deleted_at`,`user_name`,`password`)
VALUES
('2023-08-02 17:19:00.250','2023-08-02 17:19:00.250',NULL,'lps','123')
gorm ä¼Âèªå¨维æ¤ created_atãÂÂupdated_ad å deleted_at ä¸Â个åÂÂ段ãÂÂ
åªæÂÂå ¥æÂÂå®ÂçÂÂÃ¥ÂÂ段
使ç¨Select
æ¹æ³Âï¼ÂéÂÂæ©æÂÂå®ÂÃ¥ÂÂ段æÂÂÃ¥Â
¥ãÂÂ
user := User{UserName: "lps", Password: "123"}
result := db.Select("UserName").Create(&user)
çÂÂÃ¥ÂÂäºÂ以丠SQLãÂÂ
INSERT INTO `users` (`user_name`) VALUES ('lps')
éÂÂè¦Â注æÂÂï¼Â使ç¨ select æ¶ä¸Âä¼Âèªå¨维æ¤ created_atãÂÂupdated_ad å deleted_atãÂÂ
æ¹éÂÂæÂÂå ¥
å½ÂéÂÂè¦Âæ¹éÂÂæÂÂå ¥æ¶ï¼Âä¼ å ¥ä¸Â个åÂÂçÂÂå³å¯ãÂÂ
users := []User{
{UserName: "lps", Password: "123"},
{UserName: "yhy", Password: "456"},
{UserName: "hhh", Password: "789"},
}
db.Create(&users)
2. å é¤记å½Â
å é¤ä¸Âæ¡æ°æ®
使ç¨db.Delete
æ¹æ³Âï¼Âå é¤åÂÂæ¡æ°æ®ï¼ÂéÂÂè¦ÂæÂÂå®ÂIDï¼Âå¦åÂÂä¼Âæ¹éÂÂå é¤ãÂÂ
user.ID = 20
db.Delete(&user)
çÂÂÃ¥ÂÂäºÂ以ä¸ÂSqlè¯ÂÃ¥ÂÂ¥
UPDATE `users` SET `deleted_at` = '2023-08-02 17:22:32.389'
WHERE `users`.`id` = 20 AND `users`.`deleted_at` IS NULL
设置å é¤æ¡件
éÂÂ常使ç¨Where
æ¹æ³Âè¿Âè¡Âæ¡件设置ãÂÂ
db.Where("user_name = ?","lps").Delete(&user)
// DELETE FROM users WHERE user_name = `lps`;
æ ¹æ®主é®å é¤
GORM å Â许éÂÂè¿Â主é®(å¯以æ¯å¤ÂÃ¥ÂÂ主é®)Ã¥ÂÂå èÂÂæ¡件æÂ¥å é¤对象ï¼Âå®Âå¯以使ç¨æ°åÂÂï¼Âä¹Âå¯以使ç¨åÂÂ符串ï¼Â
db.Delete(&User{}, 10)
//DELETE FROM users WHERE id = 10;
db.Delete(&User{}, "10")
// DELETE FROM users WHERE id = 10;
db.Delete(&users, []int{1,2,3})
// DELETE FROM users WHERE id IN (1,2,3);
æ¹éÂÂå é¤
å¦ÂæÂÂæÂÂå®ÂçÂÂå¼ä¸Âå æ¬主å±Âæ§ï¼Âé£习GORM ä¼Âæ§è¡Âæ¹éÂÂå é¤ï¼Âå®Âå°Âå é¤æÂÂæÂÂå¹é ÂçÂÂè®°å½Â
db.Where("user_name LIKE ?", "%lps%").Delete(&User{})
// DELETE from users where email LIKE "%lps%";
å¯以å°Âä¸Â个主é®åÂÂçÂÂä¼ éÂÂç»ÂDelete
àæ¹æ³Âï¼Â以便æ´é«ÂæÂÂçÂÂå é¤æ°æ®éÂÂ大çÂÂè®°å½Â
var users = []User{{ID: 1}, {ID: 2}, {ID: 3}}
db.Delete(&users)
// DELETE FROM users WHERE id IN (1,2,3);
db.Delete(&users, "name LIKE ?", "%lps%")
// DELETE FROM users WHERE name LIKE "%lps%" AND id IN (1,2,3);
软å é¤
å¦ÂæÂÂä½ çÂÂ模åÂÂÃ¥ÂÂ
å«äºÂàgorm.DeletedAt
Ã¥ÂÂ段ï¼Â该åÂÂ段ä¹Â被åÂÂ
å«å¨gorm.Model
ä¸Âï¼Âï¼Âé£ä¹Â该模åÂÂå°Âä¼Âèªå¨è·å¾Â软å é¤çÂÂè½åÂÂï¼Â
å½Âè°Âç¨Delete
æ¶ï¼ÂGORM并ä¸Âä¼Âä»Âæ°æ®åºÂä¸Âå é¤该记å½Âï¼ÂèÂÂæ¯å°Â该记å½ÂçÂÂDeleteAt
设置为å½ÂÃ¥ÂÂæ¶é´ï¼ÂèÂÂÃ¥ÂÂçÂÂä¸Âè¾¥询æ¹æ³Âå°Âæ æ³ÂæÂ¥æ¾å°æ¤æ¡记å½ÂãÂÂ
// å é¤ä¸Â个ID为1çÂÂuseræ°æ®
db.Delete(&User{}, 1)
// Update users SET deleted_at = "2023-08-02 18:13" WHERE id = 1;
å¦ÂæÂÂ你并ä¸Âæ³åµÂå¥Âgorm.Model
ï¼Âä½ ä¹Âå¯以åÂÂä¸Âæ¹ä¾ÂÃ¥ÂÂé£样å¼Âå¯软å é¤ç¹æ§ï¼Â
type User struct {
ID int
Deleted gorm.DeletedAt
Name string
}
æÂ¥æ¾被软å é¤çÂÂæ°æ®
ä½ å¯以使ç¨Unscoped
æÂ¥æ¥询被软å é¤çÂÂæ°æ®
db.Unscoped().Where("id = 1").Find(&users)
//SELECT * FROM users WHERE id = 1;
永习å é¤
ä½ å¯以使ç¨Unscoped
æ¹æ³Âæ¥永ä¹Â
å é¤æ°æ®
db.Unscoped().Delete(&users)
//DELETE FROM users WHERE id = 1;
3. æ´æ°记å½Â
æ´æ°æÂÂæÂÂÃ¥ÂÂ段
使ç¨Save
æ¹æ³Âä¼Âæ´æ°æÂÂæÂÂÃ¥ÂÂ段,å³使æ¯é¶å¼ä¹Âä¼Âæ´æ°
db.First(&user)
user.UserName = ""
db.Save(&user)
//UPDATE users SET created_at = '2023-08-02 17:12:08.548', updated_at = '2023-08-02 17:17:40.891', deleted_at = NULL, user_name = '', password = 'ddd' WHERE id = 1
æ´æ°åÂÂ个åÂÂ
使ç¨Model
Ã¥ÂÂUpdate
æ¹æ³Âæ´æ°åÂÂÃ¥ÂÂ,å¯以使ç¨ç»ÂæÂÂä½Âä½Â为éÂÂÃ¥ÂÂæ¡件,ä»Â
éÂÂæ© ID
user.ID = 2
db.Model(&user).Update("user_name", "lps")
// UPDATE users SET user_name = lps ,updated_at = 2023-08-02 18:12:34 WHERE id = 2;
ä¹Âå¯å¨Modelä¸Â设置空ç»ÂæÂÂä½Â,使ç¨Where
æ¹æ³Âèª己éÂÂÃ¥ÂÂæ¡件
db.Model(&User{}).Where("user_name = ?","lps").Update("user_name","spl")
// UPDATE users SET user_name ='spl' ,updated_at = '2023-08-02 18:12:34' WHERE user_name =lps';
æ´æ°å¤Â个åÂÂ
使ç¨Updates
æ¹æ³Âè¿Âè¡Âæ´æ°å¤ÂÃ¥ÂÂãÂÂæ¯æÂÂstruct
Ã¥ÂÂmap
æ´æ°ãÂÂå½Âæ´æ°æ¡件æ¯struct
æ¶ï¼Âé¶å¼ä¸Âä¼Âæ´æ°ï¼Âå¦ÂæÂÂç¡®ä¿ÂæÂÂÃ¥ÂÂå¿Â
å®Âæ´æ°ï¼Â使ç¨Select
éÂÂæ©该åÂÂãÂÂ
// 使ç¨structæ´æ°,åªä¼Âæ´æ°éÂÂé¶åÂÂ段,ä¸Âé¢è¿Â个ä¾ÂÃ¥ÂÂä¸ÂPassword为é¶å¼,Ã¥ÂÂä¸Âæ´æ°
db.Model(&user).Updates(User{ID: 1,UserName: "hello", Password: ""})
// UPDATE users SET user_name='hello', updated_at = '2023-08-02 21:34:10' WHERE id = 1;
// 使ç¨mapæ´æ°,ä¸Âé¢è¿Â个ä¾ÂÃ¥ÂÂä¸ÂPassword为é¶å¼,ä¾Âç¶æ´æ°
db.Model(&user).Updates(map[string]interface{}{"ID": 1,"UserName:"hello", "Password": ""})
// UPDATE users SET user_name='hello', password = '',updated_at = '2023-08-02 21:34:10' WHERE id = 1;
æ´æ°éÂÂå®ÂÃ¥ÂÂ段
å¦ÂæÂÂè¦Âæ´æ°éÂÂå®ÂçÂÂÃ¥ÂÂ段æÂÂæ´æ°æ¶忽çÂ¥æÂÂäºÂÃ¥ÂÂ段ï¼Âå¯以使ç¨Select
(éÂÂå®Â),Omit
(忽çÂÂ¥)
//user.ID = 1
//使ç¨map
db.Model(&user).Select("user_name").Updates(map[string]interface{}{"user_name": "lps","password": "123"})
// UPDATE SET user_name = 'lps' WHERE id = 1;
db.Model(&user).Omit( "user_name" ).Updates( map [ string ] interface {}{ "user_name" : "lps" , "password" : "123"})
//UPDATE users SET password = '123', updated_at='2023-08-02 21:34:10' WHERE id = 1;
//使ç¨struct类似
æ¹éÂÂæ´æ°
å¦ÂæÂÂæÂÂ们没æÂÂæÂÂå®ÂÃ¥Â
·æÂÂ主é®å¼çÂÂè®°å½ÂModel
ï¼ÂGORMå°Âæ§è¡Âæ¹éÂÂæ´æ°
// 使ç¨ struct
db.Model(User{}).Where( "role = ?" , "admin" ).Updates(User{UserName: "hello" , Password: "123" })
// UPDATE users SET user_name='hello'ï¼Âpassword = '123' WHERE role = 'admin';
// 使ç¨mapæ´æ°
db.Table( "users" ).Where( "id IN ?" , [] int { 10 , 11 }).Updates( map [ string ] interface {}{ "user_name" : "hello" , "password" : "123" })
// UPDATE users SET user_name='hello',password = '123' WHERE id IN (10, 11);
æ´æ°çÂÂè®°å½Âæ°
使ç¨result := db.Model().Updates()
å¯以è·åÂÂÃ¥ÂÂæ´æ°影åÂÂçÂÂè¡Âæ°
result.RowsAffected //è¿ÂÃ¥ÂÂæ´æ°çÂÂè®°å½Âæ°
result.Error //è¿ÂÃ¥ÂÂæ´æ°æ¶çÂÂéÂÂ误
4. æ¥询记å½Â
æ¥询åÂÂ个对象
gorm
æÂÂä¾Â亠FirstãÂÂTakeãÂÂLast
æ¹æ³ÂãÂÂå®Â们é½æ¯éÂÂè¿ LIMIT 1
àæÂ¥å®Âç°çÂÂï¼ÂÃ¥ÂÂå«æ¯主é®åÂÂåºÂãÂÂä¸ÂæÂÂåºÂÃ¥ÂÂ主é®éÂÂåºÂãÂÂ
user := User{}
// è·åÂÂ第ä¸Âæ¡记å½Âï¼Â主é®åÂÂåºÂï¼Â
db.First(&user)
// SELECT * FROM users ORDER BY id LIMIT 1;
// è·åÂÂä¸Âæ¡记å½Âï¼Â没æÂÂæÂÂå®ÂæÂÂåºÂÃ¥ÂÂ段
db.Take(&user)
// SELECT * FROM users LIMIT 1;
// è·åÂÂæÂÂÃ¥ÂÂä¸Âæ¡记å½Âï¼Â主é®éÂÂåºÂï¼Â
db.Last(&user)
// SELECT * FROM users ORDER BY id DESC LIMIT 1;
å¦ÂæÂÂ没æÂÂæ¥询å°对象ï¼Âä¼Âè¿Âå ErrRecordNotFound
éÂÂ误ãÂÂ
result := db.First(&user)
errors.Is(result.Error, gorm.ErrRecordNotFound)
result.RowsAffected
æ ¹æ®主é®æ¥询
å¨ First/Take/Last
çÂÂå½æ°ä¸Â设置第äºÂ个åÂÂæ°ï¼Â该åÂÂæ°被认ä½Âæ¯ ID
ãÂÂå¯以éÂÂæ© int
æ string
ç±»åÂÂãÂÂ
db.First(&user, 10)
db.First(&user, "10")
éÂÂæ© string
ç±»åÂÂçÂÂÃ¥ÂÂéÂÂæ¶ï¼ÂéÂÂè¦Â注æ SQL 注åÂ
¥é®é¢ÂãÂÂ
æ¥询å¤Â个对象ï¼ÂÃ¥ÂÂ表ï¼Â
使ç¨ Find
æ¹æ³Âæ¥询å¤Â个对象ãÂÂ
users := []User{}
result := db.Find(&users)
è¿ÂÃ¥ÂÂå¼ä¼Âæ å°Âå° users
Ã¥ÂÂçÂÂä¸ÂãÂÂ
ä¾Âç¶å¯以éÂÂè¿Â访é®è¿ÂÃ¥ÂÂå¼ä¸Âç Error
å RowsAffected
Ã¥ÂÂ段è·åÂÂå¼Â常åÂÂå½±åÂÂçÂÂè¡Âå·ãÂÂ
result.Error
result.RowsAffected
设置æ¥询æ¡件 Where
gorm
æÂÂä¾ÂäºÂä¸Âè½ç Where
æ¹æ³Âï¼Âå¯以å®Âç° =ãÂÂ<>ãÂÂINãÂÂLIKEãÂÂANDãÂÂ>ãÂÂ<ãÂÂBETWEEN
çÂÂæ¹æ³Âï¼Â使ç¨ ?
æÂ¥å ä½ÂãÂÂ
db.Where("name = ?", "l").First(&user)
// SELECT * FROM users WHERE user_name = 'l' ORDER BY id LIMIT 1;
// è·åÂÂÃ¥Â
¨é¨å¹éÂ
ÂçÂÂè®°å½Â
db.Where("name <> ?", "l").Find(&users)
// SELECT * FROM users WHERE user_name <> 'l';
// IN
db.Where("name IN ?", []string{"lps", "qqq"}).Find(&users)
// SELECT * FROM users WHERE user_name IN ('lps','qqq');
// LIKE
db.Where("name LIKE ?", "%l%").Find(&users)
// SELECT * FROM users WHERE user_name LIKE '%l%';
// AND
db.Where("name = ? AND age = ?", "lzq", "aaa").Find(&users)
// SELECT * FROM users WHERE user_name = 'lzq' AND password = aaa;
// BETWEEN
db.Where("created_at BETWEEN ? AND ?", lastWeek, today).Find(&users)
// SELECT * FROM users WHERE created_at BETWEEN '2023-07-26 00:00:00' AND '2023-08-02 00:00:00';
Where å¿«éÂÂ设置æ¡件çÂÂæ¹æ³Â
ä¼ é StructãÂÂMap
å åÂÂçÂÂæ¶ï¼Âå¯以å®Âç°æ´ç®Â便çÂÂ设置æ¡件ãÂÂ
db.Where(&User{UserName:"lps", Password:"123"}).Find(&user)
db.Where(map[string]interface{}{"user_name": "lps", "password": "123"}).Find(&user)
ç»ÂæÂÂä½Âå Map
çÂÂæÂÂæÂÂå ä¹Âæ¯ç¸çÂÂçÂÂãÂÂ
两èÂÂ
å¯ä¸ÂçÂÂä¸ÂÃ¥ÂÂä¹Âå¤Âå¨亠struct
ä¸ÂçÂÂé¶å¼åÂÂ段ä¸Âä¼Âæ¥询ãÂÂæ¯Â妠0ãÂÂ""ãÂÂfalse
ãÂÂ
Ã¥ÂÂçÂÂæ¯æ¥询主é®ãÂÂ
db.Where([]int{10, 11}).Find(&user)
// SELECT FROM users WHERE users.id IN (10,11) AND users.deleted_at IS NULL;
æÂÂæÂÂçÂÂæ¥询ï¼Âgorm
é½ä¼Âé»Â认设置 tabel.deleted_at IS NULL
àæ¥询æ¡件ãÂÂ
é¤亠Where
æ¹æ³Âå¤Âï¼Âè¿ÂæÂÂÃ¥ÂÂ
èÂÂæ¥询çÂÂæ¹å¼Âï¼Âä½Âæ¯ä¸Âæ¨èÂÂÃ¥ÂÂæ¶使ç¨两ç§Âé£Âæ ¼ãÂÂ
db.Find(&user, "user_name = ?", "lps")
// SELECT * FROM users WHERE user_name = "lps";
å ¶ä»Âæ¥询 Not & Or
gorm
è¿ÂæÂÂä¾Â亠Not å Or
æ¹æ³Âï¼Âä½Âä¸Âæ¨èÂÂ使ç¨ï¼Âå 为 Where
Ã¥ÂÂæ ·å¯以å®Âç°两èÂÂ
çÂÂÃ¥ÂÂè½
db.Where("password = ?", "123").Not("user_name", "l").Or("id > ?", 10).Find(&users)
// SELECT * FROM users WHERE (( password = '123') AND user_name <> 'l' OR id > 10 AND users.deleted_at IS NULL;
éÂÂÃ¥ÂÂç¹å®ÂÃ¥ÂÂ段 Select
使ç¨ Select
æ¹æ³ÂãÂÂ
db.Select("password").Where(&User{UserName:"lps"}).Find(&user)
// SELECT password FROM users WHERE users.user_name = 'lps' AND users.deleted_at IS NULL;
å ¶ä»ÂæÂÂä½Â
æÂÂ庠Order
db.Order("user_name desc, password").Find(&users)
// SELECT * FROM users WHERE users.deleted_at IS NULL ORDER BY user_name DESC,password;
Ã¥ÂÂ页 Limit Offset
Limit
å Offset
å¯以åÂÂç¬使ç¨ï¼Âä¹Âå¯以ç»ÂÃ¥ÂÂ使ç¨ãÂÂ
db.Limit(3).Find(&users)
db.Offset(3).Find(&users)
db.Limit(2).Offset(3).Find(&users)
Ã¥ÂÂç» Group Having
æ ¹æ® username ç»Â计ç¨æ·åÂÂçÂÂéÂÂå¤ÂãÂÂ
result := []map[string]interface{}{}
db.Model(&User{}).Select("user_name, SUM( id ) AS nums").Group("user_name").Find(&result)
// SELECT user_name,SUM( id ) AS nums FROM users GROUP BY user_name;
Ã¥ÂȎ Distinct
result := []string{}
db.Model(&User{}).Distinct("user_name").Find(&result)
// SELECT DISTINCT user_name FROM users
Ã¥ÂÂãÂÂç»ÂæÂÂè¯Â
Ã¥ÂÂæ¶两天å®ÂæÂÂè¿Âç¯ÂæÂÂç« ,æÂÂæ¯æÂÂç¹èµÂæ¶èÂÂ,å 容æÂÂä»»ä½ÂéÂÂ误欢è¿ÂæÂÂåº,æ¬人ä¸Âå®ÂÃ¥ÂÂæ¶æ¹æ£ãÂÂæ¾ÂÂåªåÂÂè¿°äºÂå 个ç®ÂÃ¥ÂÂçÂÂå¢Âå æ¹æÂ¥åÂÂè½,å ³äºÂGORMçÂÂæ´å¤Âå 容åÂÂç»Âæ´æ°!
æÂÂè°¢æ¨çÂÂè§ÂçÂÂ!!!!