10-gorm-05-高级查询

文章目录

  • 1. Order
    • 1.1 简单使用
    • 1.2 重新排序
    • 2. Limit
    • 3. Offset
  • 4. Count
  • 5. Pluck
  • 6. Scan

1. Order

1.1 简单使用

age字段降序,name字段升序(默认)

db.Order("age desc, name").Find(&users)
 SELECT * FROM users ORDER BY age desc, name;

或者写成

db.Order("age desc").Order("name").Find(&users)
  • 示例
package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)
type xiShu struct {
	ID int64
	Name string
    Age int64
}

func(xiShu) TableName() string {
	return "xi_shu"
}

func main() {
	db,_ := connect()
	defer db.Close()
	var users []xiShu
    //var user xiShu
	result := db.Order("age desc, name").Find(&users)
	fmt.Println(result.Value)
}

func connect() (db *gorm.DB,err error) {
	db, err = gorm.Open("mysql", "root:40010355@tcp(127.0.0.1:3306)/crow?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		fmt.Printf(err.Error())
		defer db.Close()
	}else {
		fmt.Printf("OK\n")
		db.DB().SetMaxIdleConns(10)
		db.DB().SetMaxOpenConns(100)
		//defer db.Close()
	}
	return
}

输出

OK
&[{1 LiuBei 28} {2 GuanYu 22} {6 MaChao 20} {3 ZhangFei 20} {5 ZhuGeLiang 20} {4 ZhaoYun 18}]

1.2 重新排序

同一个表作两个排序,分别保存到结构体切片

db.Order("age desc").Find(&users1).Order("age", true).Find(&users2)
 SELECT * FROM users ORDER BY age desc; (users1)
 SELECT * FROM users ORDER BY age; (users2)
  • 示例

上例中,main函数替换为如下内容:

func main() {
	db,_ := connect()
	defer db.Close()
	var users1,users2 []xiShu
    //var user xiShu
	//result := db.Order("age desc").Find(&users1).Order("age", true).Find(&users2)
	db.Order("age desc").Find(&users1).Order("age", true).Find(&users2)
	//fmt.Println(result.Value)
	fmt.Println(users1,"\n",users2)
}

2. Limit

db.Limit(3).Find(&users)
 SELECT * FROM users LIMIT 3;

// 用 -1 取消 LIMIT 限制条件
db.Limit(10).Find(&users1).Limit(-1).Find(&users2)
 SELECT * FROM users LIMIT 10; (users1)
 SELECT * FROM users; (users2)

3. Offset

跳过前三行,取两行。

db.Limit(2).Offset(3).Find(&users)

输出

OK
[{4 ZhaoYun 18} {5 ZhuGeLiang 20}]
  • 用 -1 取消 OFFSET 限制条件(同Limit)

4. Count

func main() {
	db,_ := connect()
	defer db.Close()
	var users []xiShu
	var count int64
	db.Where("age = ?",20).Find(&users).Count(&count)
	fmt.Println(users,count)
}

输出为:

OK
[{3 ZhangFei 20} {5 ZhuGeLiang 20} {6 MaChao 20}] 3

5. Pluck

将表格中的age列单独取出作为一个集合

  • 示例(数字列)
func main() {
	db,_ := connect()
	defer db.Close()
	var users []xiShu
	var ages []int64
	db.Find(&users).Pluck("age", &ages)
	fmt.Println(ages)
}

输出

OK
[28 22 20 18 20 20]
  • 示例(字串列)
func main() {
	db,_ := connect()
	defer db.Close()
	//var users []xiShu
    //var user xiShu
	var names []string
	//db.Model(&xiShu{}).Pluck("name", &names)
	db.Find(&[]xiShu{}).Pluck("name", &names)
	fmt.Println(names)
	//fmt.Println(users)
}

也可以写成

db.Table("xi_shu").Pluck("name", &names)

输出为

OK
[LiuBei GuanYu ZhangFei ZhaoYun ZhuGeLiang MaChao]

6. Scan

func main() {
	db,_ := connect()
	defer db.Close()
	type Result struct {
		Name string
		Age  int
	}
	var result Result
	db.Table("xi_shu").Select("name, age").Where("name = ?", "LiuBei").Scan(&result)
	fmt.Println(result)
}

输出结果

OK
{LiuBei 28}

你可能感兴趣的:(#,gorm-go连接数据库,mysql,golang,数据库,go,gorm)