Home >Database >Mysql Tutorial >How Can I Query Data from Different Databases on the Same Server?

How Can I Query Data from Different Databases on the Same Server?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-19 11:57:02945browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn