Grom之数据查询

Grom之数据查询

注:本文是基于Windos系统上 gorm.io/gorm@v1.23.4、gorm.io/driver/mysql@v1.3.3进行讲解

1.First单条查询

1.1第一种

package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

//模型结构
type Student struct {
	Id   int
	Name string
	Age  int
}

func main() {
	//使用dsn连接到数据库,grom自带的数据库池
	//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
	dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
	conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
	if err != nil {
		fmt.Println(err)
	}
	conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在

	user := new(Student)          //实例化对象     
    // SELECT * FROM users ORDER BY id LIMIT 1;
	res := conn.First(user)      //将查询数据传入到对象中
	fmt.Println(res.Error)        //判断返回值的错误
	fmt.Println(res.RowsAffected) //查看返回条数
	fmt.Println("查询到的对象为", *user)
	fmt.Println("查询到的对象为", user) //相对于上面的语句建议使用这个输出

}

image-20220112095420745

运行代码前

在这里插入图片描述

1.2第二种

//以下代码也能输出以下内容
    user := Student{}  
	res := conn.First(&user)      //将查询数据传入到对象中
	fmt.Println(res.Error)        //判断返回值的错误
	fmt.Println(res.RowsAffected) //查看返回条数
	//fmt.Println("查询到的对象为", *user)  不能写成*user ,会编译失败,不能这样写
	fmt.Println("查询到的对象为", user) //相对于上面的语句建议使用这个输出

image-20220112152130455

运行代码前

在这里插入图片描述

1.3第三种

    var user = Student{}         
	res := conn.First(&user)      //将查询数据传入到对象中
	fmt.Println(res.Error)        //判断返回值的错误
	fmt.Println(res.RowsAffected) //查看返回条数
	//fmt.Println("查询到的对象为", *user)  也不能写成*user ,会编译失败,不能这样写
	fmt.Println("查询到的对象为", user) //相对于上面的语句建议使用这个输出

image-20220112152306563

2.Find全部查询

package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

//模型结构
type Student struct {
	Id   int
	Name string
	Age  int
}

func main() {
	//使用dsn连接到数据库,grom自带的数据库池
	//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
	dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
	conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
	if err != nil {
		fmt.Println(err)
	}
	conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
    
	users := []Student{} //实例化对象    
	//上面一行等效于 var users []Student =[]Studnet{}
	conn.Find(&users)    //将所有查询数据传入到对象中
	//fmt.Println(*users)   也不能写成*users ,会编译失败,不能这样写
	fmt.Println(users)   //打印所有数据

}

image-20220112095741301

运行代码前

在这里插入图片描述

3.Where条件查询

3.1=查询

package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

//模型结构
type Student struct {
	Id   int
	Name string
	Age  int
}

func main() {
	//使用dsn连接到数据库,grom自带的数据库池
	//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
	dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
	conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
	if err != nil {
		fmt.Println(err)
	}
	conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在

	user := new(Student)
	conn.Where("age=?", 22).First(user)
	fmt.Println(user)
	fmt.Println(*user)
    
}

image-20220112102110393

运行代码前

在这里插入图片描述

3.2结构体字段查询

package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

//模型结构
type Student struct {
	Id   int
	Name string
	Age  int
}

func main() {
	//使用dsn连接到数据库,grom自带的数据库池
	//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
	dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
	conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
	if err != nil {
		fmt.Println(err)
	}
	conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在

	stu := new(Student)
	conn.Where(&Student{Name: "yang"}).Find(stu) //通过结构体的Name查找
	fmt.Println(stu)
	fmt.Println(*stu)
}

image-20220112101922960

运行代码前

在这里插入图片描述

3.3or查询

3.3.1第一种
package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

//模型结构
type Student struct {
	Id   int
	Name string
	Age  int
}

func main() {
	//使用dsn连接到数据库,grom自带的数据库池
	//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
	dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
	conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
	if err != nil {
		fmt.Println(err)
	}
	conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
	stus := []Student{}
	res := conn.Where("name=? or name=?", "yang", "zhou").Find(&stus) 
	fmt.Println(res.Error)
		fmt.Println(stus)

}

image-20220112105748588

运行代码前

在这里插入图片描述

3.3.2第二种
res := conn.Where("name=?", "yang").Or("name=?", "zhou").Find(&stus) 
	fmt.Println(res.Error)
	fmt.Println(res.RowsAffected)
	fmt.Println(stus)

image-20220112105852625

运行代码前

在这里插入图片描述

3.4map查询

package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

//模型结构
type Student struct {
	Id   int
	Name string
	Age  int
}

func main() {
	//使用dsn连接到数据库,grom自带的数据库池
	//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
	dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
	conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
	if err != nil {
		fmt.Println(err)
	}
	conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在

	stu := new(Student)
	res := conn.Where(map[string]interface{}{"name": "yang", "age": 22}).Find(&stu) 
	fmt.Println(res.Error)
	fmt.Println(res.RowsAffected)
	fmt.Println(stu)
}

image-20220112101635574

运行代码前

在这里插入图片描述

4.Order排序查询

	conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
	stus := []Student{}
	res := conn.Order("id desc").Find(&stus) // desc是降序的
	fmt.Println(res.Error)
	fmt.Println(res.RowsAffected)
	fmt.Println(stus)

image-20220112110101240

运行代码前

在这里插入图片描述

5.Order多条件排序查询

5.1第一种

conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
	stu := &Student{
		Id:   5,
		Name: "yang",
		Age:  28,
	}
	re := conn.Create(stu) //向数据库中插入数据
	if re.Error != nil {   //判断是否插入数据出错
		fmt.Println(re.Error)
	}
	stus := []Student{}
res := conn.Order("name").Order("age desc").Find(&stus) //名字是默认升序的;名字相同时年龄降序
	fmt.Println(res.Error)
	fmt.Println(res.RowsAffected)
	fmt.Println(stus)

image-20220112111340768

运行代码前

在这里插入图片描述

运行代码后

image-20220112112704120

5.2第二种

stus := []Student{}
	res := conn.Order("name").Order("age").Find(&stus) //名字是默认升序的;名字相同时年龄颜色也是默认升序
	fmt.Println(res.Error)
	fmt.Println(res.RowsAffected)
	fmt.Println(stus)

image-20220112111421927

运行代码前

image-20220112112704120

6.Limit 查询

    stus := []Student{}
	res := conn.Limit(3).Find(&stus) //只查询3条数据,查询前三条数据
	fmt.Println(res.Error)
	fmt.Println(res.RowsAffected)
	fmt.Println(stus)

image-20220112112750621

运行代码前

在这里插入图片描述

7.Offset查询

stus := []Student{}
	res := conn.Limit(3).Offset(1).Find(&stus) //跳过第一条后,查询前3条数据
	fmt.Println(res.Error)
	fmt.Println(res.RowsAffected)
	fmt.Println(stus)

image-20220112112926361

运行代码前

在这里插入图片描述

8.Distinct查询

    stus := []Student{}
	res := conn.Distinct("name").Find(&stus)
	fmt.Println(res.Error)
	fmt.Println(res.RowsAffected)
	fmt.Println(stus)
//查看name ,并且不看重复值

image-20220112130615960

9.FirstOrCreate创建并查询

	stu := Student{}
	res := conn.FirstOrCreate(&stu, Student{Name: "FirstorCreate"})
	fmt.Println(res.Error)
	fmt.Println(res.RowsAffected)
	fmt.Println(stu)

image-20220112132752093

运行代码前

image-20220112112704120

运行代码后

image-20220112132925245

10.Not查询

10.1第一种

stus := []Student{}
	conn.Not("name = ?", "yang").First(&stus)
	// 等效于:  SELECT * FROM users WHERE name <> "yang" LIMIT 1;
	fmt.Println(stus)

	conn.Not("name = ?", "xin").First(&stus)
	// 等效于:  SELECT * FROM users WHERE name <> "xin" LIMIT 1;
	fmt.Println(stus)

image-20220115194745906

运行代码前

image-20220112112704120

10.1第二种

	stus := []Student{}
	conn.Not(map[string]interface{}{"name": []string{"yang", "zhou"}}).Find(&stus)
	// SELECT * FROM users WHERE name NOT IN ("yang", "zhou");
	fmt.Println(stus)

	conn.Not(map[string]interface{}{"name": []string{"jun", "xin"}}).Find(&stus)
	// SELECT * FROM users WHERE name NOT IN ("jun", "xin");
	fmt.Println(stus)

image-20220115201328849

运行代码前

image-20220112112704120

10.1第三种

	stus := []Student{}
	conn.Not(Student{Name: "yang", Age: 22}).First(&stus)
	// SELECT * FROM users WHERE name <> "yang" AND age <> 22 ORDER BY id LIMIT 1;
	fmt.Println(stus)

	conn.Not(Student{Name: "zhou", Age: 22}).First(&stus)
	// SELECT * FROM users WHERE name <> "zhou" AND age <> 22 ORDER BY id LIMIT 1;
	fmt.Println(stus)

image-20220115200248406

运行代码前

image-20220112112704120

10.1第四种

    stus := []Student{}
	conn.Not([]int64{1, 2, 3}).First(&stus)
	// SELECT * FROM users WHERE id NOT IN (1,2,3) ORDER BY id LIMIT 1;
	fmt.Println(stus)
	fmt.Println()

	conn.Not([]int64{1, 2}).First(&stus)
	// SELECT * FROM users WHERE id NOT IN (1,2) ORDER BY id LIMIT 1;
	fmt.Println(stus)

image-20220115200500244

运行代码前

image-20220112112704120

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GoGo在努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值