postgresql - 在 Golang 上使用 LEFT JOIN 并映射到结构
问题描述
我在 Go 上有以下结构:
type Struct1 struct {
ID int64 `db:id`
InternalID int64 `db:internal_id`
Structs2 []Struct2
}
type Struct2 struct {
ID int64 `db:id`
InternalID int64 `db:internal_id`
SomeText string `db:some_text`
}
这两者的关系是:Struct1只能有一个,通过internal_id连接N个Struct2。所以我正在做这个查询:
SELECT*
FROM struct1 st1
LEFT JOIN struct2 st2
ON
st1.internal_id = st2.internal_id
LIMIT 10
OFFSET (1 - 1) * 10;
通过在 Go 上执行此查询,我想知道我是否可以:通过正确映射数组 Struct2 来创建一个 Struct1 数组。如果有可能,我该怎么做?提前致谢。
我正在使用 Postgres 和 sqlx。
解决方案
这完全取决于您用于连接数据库的库/客户端,但是,我从未见过库支持您尝试执行的操作,因此我将提供您可以执行的自定义实现。完全披露,我没有对此进行测试,但我希望它能给你一个大致的想法,任何人都应该随时添加编辑。
package main
type Struct1 struct {
ID int64 `db:id`
InternalID int64 `db:internal_id`
Structs2 []Struct2
}
type Struct2 struct {
ID int64 `db:id`
InternalID int64 `db:internal_id`
SomeText string `db:some_text`
}
type Row struct {
Struct1
Struct2
}
func main() {
var rows []*Row
// decode the response into the rows variable using whatever SQL client you use
// We'll group the struct1s into a map, then iterate over all the rows, taking the
// struct2 contents off of rows we've already added to the map and then appending
// the struct2 to them. This effectively turns rows into one-to-many relationships
// between struct1 and struct2.
mapped := map[int64]*Struct1{}
for _, r := range rows {
// The key of the map is going to be the internal ID of struct1 (that's the ONE
// in the one-to-many relationship)
if _, ok := mapped[r.Struct1.InternalID]; ok {
// Make sure to initialize the key if this is the first row with struct1's
// internal ID.
mapped[r.Struct1.InternalID] = &r.Struct1
}
// Append the struct 2 (the MANY in the one-to-many relationship) to the struct1s
// array of struct2s.
mapped[r.Struct1.InternalID].Structs2 = append(mapped[r.Struct1.InternalID].Structs2, r.Struct2)
}
// Then convert it to a slice if needed
results := make([]*Struct1, len(mapped))
i := 0
for _, v := range mapped {
results[i] = v
i++
}
}
推荐阅读
- frida - 如何在android中打印调用的函数?
- mysql - 更改 spring jpa 现有表列类型
- c++ - 如何从平面缓冲区中反序列化联合结构的 void* 值的大小
- python-3.x - 我应该如何从 Python 中的字符串自动创建变量?
- r - 我想创建一个按订单 ID 分组的最大日期列
- sql - 访问查询:无法获得正确的计数
- kernel - 如何使用 seccomp_release libseccomp?
- swift - “协议”类型的值没有成员“功能”
- node.js - 将图像上传到您的网站 Node js 的正确方法是什么
- android - 如何检测该应用程序是否在测试版 Android 上运行?