// db.Take(dest interface{}, conds ...interface{}) // 这个接口和db.First()类似,但是它查询的时候不是排序的,如果给定了条件,则会先根据条件查询符合条件的第一条记录,然后将其赋值给dest参数的结构体,然后返回。 // 例如查询第一个Sex为1(男)的记录 var u User db.Take(&u, "Sex = ?", 1) // SELECT * FROM user WHERE Sex = 1 LIMIT 1;
// db.Last(dest interface{}, conds ...interface{}) // 这个接口是根据主键查询(满足条件)最后一条记录 // 例如查询满足Sex为1(男)的最后一条记录 db.Last(&u, "Sex = ?", 1) // 对应的sql语句是: SELECT * FROM user WHERE Sex = 1 ORDER BY id DESC LIMIT 1;
// 例如查询第一个名字为Alice的记录 db.Where("name = ?", "Alice").First(&u) // 对应的sql语句是: SELECT * FROM user WHERE name = 'Alice' LIMIT 1;
// 得到所有满足pwd为123456的记录 db.Where("pwd = ?", "123456").Find(&u) // 对应的sql语句是: SELECT * FROM user WHERE pwd = '123456';
// 得到所有不满足name为Alice的记录 db.Where("name <> ?", "Alice").Find(&u) // 对应的sql语句是: SELECT * FROM user WHEARE name <> 'Alice';
// 得到所有name满足一个查询数组条件的记录 db.Where("name IN (?)", []string{"Alice", "Bob"}).Find(&uu) // 对应的sql语句是: SELECT * FROM user WHERE name IN ('Alice', 'Bob')
// 查询名字为%A%的所有记录 db.Where("name LIKE ?", "%A%").Find(&uu) // 对应的sql语句是: SELECT * FROM user WHERE name LIKE '%A%'
// 查询名字为Alice同时密码为123456的记录 db.Where("name = ? AND pwd = ?", "Alice", "123456").Find(&uu) // 对应的sql语句是: SELECT * FROM user WHERE name = 'Alice' AND pwd = '123456'
// 查询更新时间为上周的记录 db.Where("updated_at > ?", time.Now().Add(-7*24*time.Hour)).Find(&uu) // 对应的sql语句是: SELECT * FROM user WHERE updated_at > '2024-11-24 00:00:00'
// 查询创建时间位于一个区间之间的记录 db.Where("created_at BETWEEN ? AND ?", "2021-01-01 00:00:00", "2022-01-01 00:00:00").Find(&uu) // 对应的sql语句是: SELECT * FROM user WHERE created_at BETWEEN '2021-01-01 00:00:00' AND '2022-01-01 00:00:00'
Stuct以及Map查询
1 2 3 4 5 6 7 8 9 10 11 12 13
var uuu User // 使用结构体查询 db.Where(User{Name: "Alice", Pwd: "123456"}).Find(&uuu)
fmt.Println("查询结果为: ", uuu)
var uuuu User // 使用map来查询 db.Where(map[string]interface{}{"name": "Bob", "pwd": "654321"}).Find(&uuuu)
// db.Not(query interface{}, args ...interface{}) // 第一个为查询的条件,第二个参数是条件的具体值,可以为空,表示无参数 /* 使用Not()时候有两种区别: 1. 使用name = ? 作为条件,会被翻译成 NOT (name = 'Alice') 这种形式,例如: db.Not("name = ?", "Alice").Find(&u) 对应的sql语句是: SELECT * FROM user WHERE NOT (name = 'Alice'); db.Not("name = ?", []string{"Alice", "Bob"}).Find(&u) 其对应的sql语句是 SELECT * FROM user WHERE NOT name = ('Alice', 'Bob') 这种写法是错的,所以编译器会报错,这是就得使用下面这种方式 2. 翻译成NOT IN的形式,例如: db.Not("name", []string{"Alice", "Bob"}).Find(&u) 对应的sql语句是: SELECT * FROM user WHERE name NOT IN ('Alice', 'Bob') */
// 得到所有name不为Alice的记录 db.Not("name = ?", "Alice").Find(&uu) // 对应的sql语句是 SELECT* FROM user WHERE NOT (name = 'Alice')
// 得到所有name不为Alice且pwd不为123456的记录 db.Not("name = ? AND pwd = ?", "Alice", "123456").Find(&uu) // 对应的sql语句是 SELECT* FROM user WHERE NOT (name = 'Alice' AND pwd = '123456')
// 得到name不在切片([]string{"Alice", "Bob"})中的记录 db.Not("name", []string{"Alice", "Bob"}).Find(&uu) // 对应的sql语句是 SELECT* FROM user WHERE name NOT IN ('Alice', 'Bob')
// 使用结构体来查询 db.Not(User{Name: "Alice", Pwd: "123456"}).Find(&uu) // 对应的sql语句是 SELECT * FROM user WHERE NOT (name = 'Alice' AND pwd = '123456')
// 直接使用Or()方法,例如: db.Or("name = ?", "Alice").Or("name = ?", "Bob").Find(&uu) // 对应的sql语句是: SELECT * FROM user WHERE name = 'Alice' OR name = 'Bob'
// 使用Or()方法和Where()方法组合,例如: db.Where("name = ?", "Alice").Or("name = ?", "Bob").Find(&uu) // 对应的sql语句是: SELECT * FROM user WHERE name = 'Alice' OR name = 'Bob'
// 使用结构体 db.Where("name = 'Alice'").Or(User{Name: "Bob", Pwd: "123456"}).Find(&uu) // 对应的sql语句是 SELECT * FROM `user` WHERE name = 'Alice' OR (`user`.`name` = 'Bob' AND `user`.`pwd` = '654321')
// 使用MAP db.Where("namse = 'Alice'").Or(map[string]interface{}{"name": "Bob", "pwd": "654321"}).Find(&uu) // 对应的sql语句是 SELECT * FROM `user` WHERE name = 'Alice' OR (`name` = 'Bob' AND `pwd` = '654321')
// ---------------------------db.Attrs()方法----------------------------- // 如果未找到,将使用参数初始化结构体,并创建一条新的记录 // 未找到 db.Where(User{Name: "non_existing"}).Attrs(User{Age: 20}).FirstOrCreate(&user) //// SELECT * FROM users WHERE name = 'non_existing'; //// INSERT INTO "users" (name, age) VALUES ("non_existing", 20); //// user -> User{Id: 112, Name: "non_existing", Age: 20}
// 找到 db.Where(User{Name: "jinzhu"}).Attrs(User{Age: 30}).FirstOrCreate(&user) //// SELECT * FROM users WHERE name = 'jinzhu'; //// user -> User{Id: 111, Name: "jinzhu", Age: 20}
// ---------------------------db.Assign()方法------------------------------ // 未找到 db.Where(User{Name: "non_existing"}).Assign(User{Age: 20}).FirstOrCreate(&user) //// SELECT * FROM users WHERE name = 'non_existing'; //// INSERT INTO "users" (name, age) VALUES ("non_existing", 20); //// user -> User{Id: 112, Name: "non_existing", Age: 20}
// 找到 db.Where(User{Name: "jinzhu"}).Assign(User{Age: 30}).FirstOrCreate(&user) //// SELECT * FROM users WHERE name = 'jinzhu'; //// UPDATE users SET age=30 WHERE id = 111; //// user -> User{Id: 111, Name: "jinzhu", Age: 30}
/* 首先是sql语句: SELECT department, SUM(salary) AS total_salary FROM employees GROUP BY department HAVING total_salary > 50000 ORDER BY total_salary DESC; */
// 然后是Gorm的写法:
var emp []struct { Department string Total_salary float64 }
var employee []employees.Employee db.Table("employees").Find(&employee) fmt.Println("员工数据:", employee)
db.Table("employees").Select("department, SUM(salary) as total_salary").Group("department").Having("total_salary > ?", 50000).Order("total_salary DESC").Scan(&emp)
fmt.Println("查询结果为: ", emp)
Joins
作用: 用于连接多个表。
1 2 3 4 5 6 7 8 9 10
rows, err := db.Table("employees").Joins("INNER JOIN user ON employees.id = user.id").Where("user.name = ?", "Alice").Or("user.name = ?", "Bob").Rows() // 对应的sql语句是: SELECT * FROM employees INNER JOIN user ON employees.id = user.id WHERE user.name = 'Alice' OR user.name = 'Bob'; // 也可以是left join right join for rows.Next() { var e employees.Employee db.ScanRows(rows, &e) employee = append(employee, e) }
fmt.Println("员工数据:", employee)
Pluck
Pluck,查询 model 中的一个列作为切片,如果您想要查询多个列,您应该使用 Scan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 通过 GORM 查询用户 var uu []user.User var names []string