Home >Database >Mysql Tutorial >How Can I Query Data from Different Databases on the Same Server?
Querying Different Databases within a Server
It is possible to perform select statements (as well as inserts) across distinct databases residing on the same server. This enables the efficient retrieval or manipulation of data spanning multiple database instances.
Syntax for Database Specification
To specify the desired database, use the following syntax within your query statement:
databasename.tablename
Example Query
Consider the following example, where we retrieve user IDs from two databases (mydatabase1 and mydatabse2):
SELECT mydatabase1.tblUsers.UserID, mydatabase2.tblUsers.UserID FROM mydatabase1.tblUsers INNER JOIN mydatabase2.tblUsers ON mydatabase1.tblUsers.UserID = mydatabase2.tblUsers.UserID
This query will retrieve and display the UserID column from both mydatabase1.tblUsers and mydatabase2.tblUsers, where the UserID values match.
The above is the detailed content of How Can I Query Data from Different Databases on the Same Server?. For more information, please follow other related articles on the PHP Chinese website!