-2

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

yeshanmao
  • 1
  • 1
  • Does this answer your question? https://stackoverflow.com/q/5698378/13860 – Flimzy Jan 26 '21 at 09:53
  • no ,the problem is how to excute the sql with GORM – yeshanmao Jan 26 '21 at 10:05
  • Can you note which version of the gorm package you are using? So you want to connect to a single mysql instance which contains two databases, db1 and db2, and make a join between tables in these two. Is that right? – Ezequiel Muns Jan 26 '21 at 10:19
  • yes , and it's solved by setting dbdatabase="" to init the parameter addr. The version should do not matter – yeshanmao Jan 27 '21 at 05:22

1 Answers1

0

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.

yeshanmao
  • 1
  • 1