Home >Database >Mysql Tutorial >Can I perform cross-database queries and inserts in a single SQL statement?

Can I perform cross-database queries and inserts in a single SQL statement?

Barbara Streisand
Barbara StreisandOriginal
2024-11-15 19:46:031063browse

Can I perform cross-database queries and inserts in a single SQL statement?

Cross-Database Queries and Inserts

Can you perform data retrieval (SELECT) or manipulation (INSERT) operations across multiple databases housed on the same server?

Yes, you can! To do so, utilize the following syntax:

databasename.tablename

For instance, let's say we have two databases, "mydatabase1" and "mydatabase2," each with a table named "tblUsers." To retrieve data from both tables, we would use the following query:

SELECT 
    mydatabase1.tblUsers.UserID, 
    mydatabse2.tblUsers.UserID
FROM 
   mydatabase1.tblUsers
       INNER JOIN mydatabase2.tblUsers 
           ON mydatabase1.tblUsers.UserID = mydatabase2.tblUsers.UserID

The above is the detailed content of Can I perform cross-database queries and inserts in a single SQL statement?. 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