Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable cross-database joins in mysql?

I am trying to port some data over from my production database to my sandbox using a query like this:

INSERT `dbsandbox`.`SomeTable`(Field1, Field2, Field3)
SELECT t.Field1, t.Field2, t.Field3
FROM `dbprod`.`SomeTable` t;

When I attempt this cross-database join I get the following error:

ERROR 1142 (42000): SELECT command denied to user 'myusername'@'server.domain.tdl' for table 'SomeTable'

The user in question has permission to the tables in question for both databases. I have tried this in both the unix mysql client and the windows MySQL Query Browser application with the same result.

What am I missing?

like image 591
Chris Avatar asked Dec 03 '08 18:12

Chris


People also ask

How do I join cross database?

Select the file or database that you want to connect to, then double-click or drag a table to the canvas. In the left pane, under Connections, click the Add button ( in web authoring) to add your second connection to the Tableau data source. The Cross-database join option is displayed.

What is cross join in MySQL?

MySQL CROSS JOIN is used to combine all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. The CROSS JOIN is also known as CARTESIAN JOIN, which provides the Cartesian product of all associated tables.

What is cross DB join?

A cross database join allows you to join data from two different types of databases as if they were in the same database.

Which of the following type of database does not support cross database join?

Specifically, you cannot use cross-database joins with these connection types: Tableau Server. Firebird.


2 Answers

It turns out it was a permissions problem. The source database required a password for the username I was using in order to access any tables. The target only required the username be on the localhost.

Even though I launch the MySQL client using a password every time within the context of a cross-database query another connection is attempted. This connection does not remember that I originally authenticated against the client with a password so connecting from the sandbox to the production database failed. Apparently explicitly stating the database name for a table sometimes implies the need for another connection, as well.

The answer was to initiate the query from with the production database, refer to the local tables without the database qualifier, then insert across to the sandbox database tables. This time it worked:

USE dbprod

INSERT dbsandbox.SomeTable(Field1, Field2, Field3) SELECT t.Field1, t.Field2, t.Field3 FROM SomeTable t;

like image 162
Chris Avatar answered Sep 28 '22 02:09

Chris


It sounds like a permissions problem. Often user permissions are set up on a database in a database fashion, so the user for the destination doesn't have access to the source.

First make sure that you can do the select from the source database.

SELECT t.Field1, t.Field2, t.Field3
FROM `dbprod`.`SomeTable` t;

Does that work? If not, then you need to grant the user select permissions on the source db.

Both databases are on/in the same server right?

like image 34
Rob Prouse Avatar answered Sep 28 '22 02:09

Rob Prouse