views:

41

answers:

2

how can i run a query that joins two tables from TWO different Databases in mssql_query or mysql_query in php for example

$conn=mssql_connect($ip,$username,$password);
mssql_select_db("DB1",$conn);
$q="select A.name,B.ID from DB1.dbo.T1 A, DB2.dbo.T2 B where A.ID=B.ID";
$res=mssql_query($q);

how to run such query??

+2  A: 

Just prefix the tablenames with the database name, as you are already doing.

The user login that you are using to connect to mySQL needs to have access to both databases. Without this, it is impossible.

Pekka
A: 

I think something like this:


SELECT X.field1, Y.field2
FROM database1.table_a AS X
INNER JOIN database2.table_b as Y
ON X.id=Y.id

[EDITED] Sorry I didn't finish the post, you should use mysqli http://www.php.net/manual/en/mysqli.query.php (don't worry for the constructor, put just 1 database) and run the query as a regular query. Also, like the guy in the top said, the user that makes the query must have the permissions for both tables.

pablo89