gpt4 book ai didi

sql-server - 使用GORM从GOlang中的SQL处理多个结果集

转载 作者:行者123 更新时间:2023-12-01 20:26:27 31 4
gpt4 key购买 nike

之前的类似问题可以追溯到2012年,当时还没有解决方案,因此我不得不重新提出。

struct type DualTable{
table1 []Table1
table2 []Table2
}

struct type Table1{
A string
B string
}

struct type Table2{
P string
Q string
}

var dualtable []DualTable
var table1 []Table1
var table2 []Table2

func main(){
//trial 1 :failed as i get only table1 result
db.Raw("select * from table1 select * from table2").Scan(&table1).Scan(&table2)

//trial 2 :failed as i get only table2 result
db.Raw("select * from table2 select * from table1").Scan(&table1).Scan(&table2)

//trial 3 : failed as got nothing
db.Raw("select * from table1 select * from table2").Scan(&dualtable)
}

如您所见,我正在尝试做。
我试图在DualTable结构中获得两个表的结果
但是似乎只有第一个查询在运行。

实际的代码包含非常长的结构,并且是 secret 的,因此我无法在此处发布。

最佳答案

您需要在两个单独的请求中执行此操作。

var dualtable DualTable
db.Raw("select * from table1").Find(&dualtable.table1)
db.Raw("select * from table2").Find(&dualtable.table2)

更新:

您还可以将 table1table2嵌入结构中,并将该结构传递给 Scan:
type DualTable struct {
Table1 //embedded
Table2 //embedded
}
var dt []DualTable
db.Raw(/*you query here*/).Scan(&dt)

关于sql-server - 使用GORM从GOlang中的SQL处理多个结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61859911/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com