使用 GORM 连接和遍历结果行的方法
来源:stackoverflow
时间:2024-02-17 10:54:24 325浏览 收藏
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《使用 GORM 连接和遍历结果行的方法》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。
问题内容
使用 gorm,我为 users
和 product_prices
表创建了以下模型
// model for table `users` type user struct { id uint64 `json:"id" gorm:"primarykey"` email string `json:"email" gorm:"unique"` password []byte `json:"-"` createdon time.time `json:"created_on" gorm:"<-:create"` updatedon time.time `json:"updated_on"` } // model for table `product_prices` type productprice struct { id uint64 `json:"id" gorm:"primarykey"` price float64 `json:"price"` direction string `json:"direction"` notificationmethod string `json:"notification_method"` createdon time.time `json:"created_on,omitempty" gorm:"<-:create"` updatedon time.time `json:"updated_on,omitempty"` lastnotified time.time `json:"last_notified,omitempty"` userid uint64 `json:"user_id"` user user `json:"-" gorm:"foreignkey:userid"` }
我想做一些类似在 product_prices
和 users
表之间执行以下 sql 连接
select product_prices.id, product_prices.price, product_prices.created_on, users.email as user_email, users.created_on as user_created_on from product_prices join users on product_prices.user_id=users.id;
这是我尝试过的许多事情之一,但仍然不走运,因为文档页面缺乏清晰度 https://gorm.io/docs/query.html#joins 我还想迭代行
按照此处的文档 https://gorm.io/docs/query.html#joins 我尝试了以下操作
// struct for result of join query type ProductPriceUser struct { Id uint64 `json:"id"` Price float64 `json:"price"` CreatedOn time.Time `json:"created_on,omitempty"` UserEmail User `json:"user_email"` UserCreatedOn User `json:"user_created_on"` } var productPrice ProductPrice database.DB.Model(&productPrice{}).Select("productPrices.id, productPrices.price, productPrices.created_on, users.email as users_email, users.created_on as users_created_on").Joins("join user on productPrices.user_id = users.id").Scan(&ProductPriceUser{}) for _, row := range ProductPriceUser { fmt.Println("values: ", row.Id, row.Price, row.CreatedOn, row.UserEmail, row.UserCreatedOn, "\n") }
但是遇到了各种各样的错误,我做错了什么以及如何获得上面描述的我想要的东西?
谢谢!
正确答案
一种可能的解决方案是执行类似的操作(假设您的数据库表名为 product_prices
和 users
):
list := []ProductPriceUser{} err := database.DB.Table("product_prices"). Join("JOIN users ON users.id=product_prices.user_id"). Select("product_prices.id, product_prices.price, product_prices.created_on, users.email as user_email, users.created_on as user_created_on"). Find(&list).Error if err != nil { // handle error }
然后,您可以迭代 list
以获取每条记录。
理论要掌握,实操不能落!以上关于《使用 GORM 连接和遍历结果行的方法》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习