views:

50

answers:

2

I am using php and mysql.

i have a question, lets say, I have a userid column in users table under database A, and userid column in purchases table under database B.

Can I execute a single query, using innerjoin, to get value from 2 databases? Or I must use multiple queries?

Oh ya, if let say, i have this variable:

$conn // connect to database A

Can i create another variable to connect database B before mysql_close()??

Sorry for multiple questions here ;p

+1  A: 
SELECT * FROM database1.table1 t1, database2.table2 t2 WHERE t1.id = t2.id

from MySQL documentation

You can refer to a table within the default database as tbl_name, or as db_name.tbl_name to specify a database explicitly. You can refer to a column as col_name, tbl_name.col_name, or db_name.tbl_name.col_name. You need not specify a tbl_name or db_name.tbl_name prefix for a column reference unless the reference would be ambiguous. See Section 8.2.1, “Identifier Qualifiers”, for examples of ambiguity that require the more explicit column reference forms.

RageZ
how to connect 2 databases? I mean, in php, mysql_select_db, can i select 2 databases???? My 2 databases has different users and password
mysqllearner
you just select one of the db, it doesn't change anything, just the db you have selected you wouldn't have to write `database1.table` but just `table`. For the record: you can even select a different database but you would have to write database name on all queries.
RageZ
Thanks I will give it a try. Give me 5 minutes to test a simple query
mysqllearner
got this error: SELECT command denied to ...
mysqllearner
I must grant the privilleges to that user first?
mysqllearner
yes you have to access to both DBs with that user.
RageZ
A: 

You have to use <databasename>.<tablename> to access tables from specific database and then perform normal join.

Xinus