PHP Mysql joins across databases PHP Mysql joins across databases sql sql

PHP Mysql joins across databases


you have to alias the table in the other database:

select a.id     , b.foreign_id  from database1.table1 a  join database2.table1 b    on b.foreign_id = a.id


If the 2 databases are on the same server, you can simply qualify the table with the database name in the join query.

select * from database1.table1 as t1 inner join database2.table2 as t2 on t2.fk_id = t1.pk_id


EDIT: So what becomes of these lines?:

$conn = mysql_connect('localhost','username','password'); @mysql_select_db('database_name',$conn)

The mysql_select_db is optional. As it just selects the active db to use.http://us3.php.net/manual/en/function.mysql-select-db.php

And yes.. this is an old app, hence the mysql instead of mysqli. Suppose I could convert it.

IMHO I don't think using MYSQL has anything to do with being slow, from my own research into it, there is not much speed improvement with MYSQLi vs MYSQL. Real world tests don't show that being a big performance driver. I've honestly stuck with MYSQL for all my apps.