开发者

Can i execute a single query on 2 database?

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? O开发者_StackOverflow社区r 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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜