GoLang: 使用两个不同的连接/数据库连接JOIN SQL查询

huangapple go评论66阅读模式
英文:

GoLang: JOIN sql with two different connections / databases

问题

我使用GoLang。我有两个不同的数据库服务器:
1)Postgresql
2)MS SQL。

对于它们中的每一个,我都建立了一个连接:

db1, err := sql.Open("postgres", psqlconn)
db2, err := sql.Open("sqlserver", u.String())

连接工作正常。

现在我想要连接第一个数据库的数据和第二个数据库:使用JOIN操作。
示例:

rows, err := db1.Query('select *
    from db1.dbname1.tabel
    left join db2.dbname2.tabel on db2.dbname2.tabel.id = db1.dbname1.tabel.id;')

但是这样不起作用 GoLang: 使用两个不同的连接/数据库连接JOIN SQL查询 如何将这两个表连接在一起?我一直没有进展。或者还有其他方法可以做到这一点吗?

谢谢你的支持!
Matthias

英文:

I use GoLang. I have two different DB servers:

  1. Postgresql
  2. MS SQL.

For both of them I make a connection each:

db1, err := sql.Open("postgres", psqlconn)
db2, err := sql.Open("sqlserver", u.String())

The connections are working fine.

Now I want to connect data of the first DB with the second DB: using JOIN.
Example:

rows, err := db1.Query('select *
    from db1.dbname1.tabel
    left join db2.dbname2.tabel on db2.dbname2.tabel.id = db1.dbname1.tabel.id;')

But this does not work GoLang: 使用两个不同的连接/数据库连接JOIN SQL查询 How can I JOIN the two tables together? I am not getting anywhere. Or is there another way to do this?

Thanks for your support!!
Matthias

答案1

得分: 2

ORM(对象关系映射)如gorm基本上是一个对象关系层,提供一些API,可以将你的语言级别的实体转换为SQL查询,反之亦然。它们基本上将你的查询转换为一个单独的SQL查询,现在你的PostgreSQL数据库接收到一个查询,其中包括表a(存在于同一数据库)和另一个表b(存在于另一个数据库)的连接!连接是在数据库层面完成的,而不是在ORM层面完成的。因此,你无法实现这个。

你可以做的最好的事情是在应用层面或其他地方进行两个单独的查询,然后将它们连接起来。

英文:

ORMs like gorm are basically an object relational layer, that provide some APIs that can translate your language level entities to SQL queries and vice versa. They basically translate your query in a single SQL query, now your postgresql database receives a query, which is a join of table a (which happens to exist) and another table b which is in another database! Joining is done in database level, not the ORM level. So you cannot achieve this.

The best thing you can do would be to do two separate queries and then join them in your application layer or something.

答案2

得分: 0

你应该能够使用PostgreSQL到MSSQLSERVER的外部数据包装器(https://github.com/tds-fdw/tds_fdw)将数据库链接在一起,或者使用MSSQLSERVER的等效功能进行反向链接。然后,你可以将组合查询发送到其中一个数据库以执行。

或者,你可以将两个数据集都获取到GO中,并在那里使用哈希表和循环等方式进行连接。如果数据集太大无法放入内存,那将会非常麻烦。

英文:

You should be able to link the databases together using a foreign data wrapper from PostgreSQL to MSSQLSERVER (https://github.com/tds-fdw/tds_fdw) or in the reverse direction using whatever it is that MSSQLSERVER call their equivalent feature. Then you would send the combined query to just one of the databases for it to execute.

Or you could fetch both datasets into GO and join them there, using hash tables and for loops or something. If the datasets are too large to fit into memory, that would be pretty annoying.

huangapple
  • 本文由 发表于 2022年7月26日 21:59:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/73124758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定