使用 GORM 处理 GO lang 中 sql 的多个结果集

Raj*_*nji 4 sql-server api struct go go-gorm

之前类似的问题可以追溯到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)
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我正在尝试做什么。
我试图在 DualTable 结构中获取两个表结果
,但似乎只有第一个查询运行。

实际的代码由非常长的结构组成,并且是机密的,因此我无法将其发布在这里。

Raj*_*nji 5

我想回答我自己的问题,因为我在这里没有找到解决方案,但不知何故找到了这个问题的解决方案,

直到2016年你都做不到。

但 GO 开发人员已向 GO 开发人员强调了这种情况,因为这可能主要发生在执行发出多个结果集的存储过程时。

所有详细信息请参见: https://go-review.googlesource.com/c/go/+/30592/

简短摘要:查询首先为您提供第一个结果集,您可以从中获取结果。一旦你完成了它。使用

这将简单地从第一个查询结果集切换到下一个结果集。然后您可以从第二个查询中获取结果。

另外,据我所知,GORM 没有这个功能。所以我跳过使用 GORM ORM。