Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

how to excute conjunctive query crossing two mysql databases with gorm?

code like:

addr:=fmt.Sprintf(`%v:%v@tcp(%v:%v)/(%v,%v)?charset=utf8`, dbuser, dbpassword, dbhost, dbport, dbdatabase)

DB, err = gorm.Open("mysql", addr)

sql := "select * from db1.user join db2.salary"
 
rows, err := DB.Raw(sql).Rows()

it seems the method gorm.Open() only accept one source parameter, and it run error "unknown table name 'db1.user'"

is there a correct way to init the DB to excute the sql or other way to solve the problem?

many thanks

question from:https://stackoverflow.com/questions/65899056/how-to-excute-conjunctive-query-crossing-two-mysql-databases-with-gorm

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

sovled by setting dbdatabase="" , which it means giving null database name connecting to the mysql instance. And database name shoud be presented as prefix on table name in sql.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...