4

Say I have two databases db1 and db2, and I have full privileges to access them.
But when I issue the following sql:

SELECT * FROM `db1.tbl1` AS t1 JOIN `db2.tbl2` AS t2 ON t1.id=t2.id

I get this error:

Table `db1.db1.tbl1` doesn't exist.

db1.tbl1 does exist. It seems mysql automatically add database name as prefix. What am I doing wrong?

adamsmith
  • 5,759
  • 4
  • 27
  • 39

2 Answers2

4

You should backtick quote the database name and table name separately:

SELECT * FROM `db1`.`tbl1` AS t1 JOIN `db2`.`tbl2` AS t2 ON t1.id=t2.id

Or just without backticks if there is no reserve name.

SELECT * FROM db1.tbl1 AS t1 JOIN db2.tbl2 AS t2 ON t1.id=t2.id
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

You need to do this:

SELECT * FROM `db1`.`tbl1` AS t1 JOIN `db2`.`tbl2` AS t2 ON t1.id=t2.id;

You need to add the ` till with the both table name and database name.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75