問題描述
我有 3 個數據庫,現在我需要將每個數據庫中的多個表連接到單個查詢中.我該怎么做?
I have 3 databases, and now I need to join several tables from each one of them into a singel query. How do I do this?
這是我的連接:
try {
$con_options = array(
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // _SILENT (pub) || _WARNING || _EXCEPTION (dev)
);
$con_1 = new PDO('mysql:host=localhost; dbname=database_1', 'user_1', 'pass_1', $con_options);
$con_2 = new PDO('mysql:host=localhost; dbname=database_2', 'user_2', 'pass_2', $con_options);
$con_3 = new PDO('mysql:host=localhost; dbname=database_3', 'user_3', 'pass_3', $con_options);
} catch (PDOException $err) {
// catch, record/log and do stuff with errors
}
我有 3 個不同的用戶,每個數據庫都有一個唯一的密碼.一個數據庫存儲 facebook-apps 和其他 iframe 應用程序的應用程序數據.另一個保存所有網上商店數據,如產品、訂單、客戶等,而第三個保存網站結構和內容.
I have 3 different users with a unique password for each database. One database stores application-data for facebook-apps and other iframe applications. Another one holds all webshop data like products, orders, customers etc. while the third one holds site structure and content.
現在;我想以某種方式在單個查詢中 JOIN
將它們三個放在一起.
Now; I would like to JOIN
the three of them together in a single query somehow.
在我寫這個問題的時候;我的一個想法是讓另一個超級"用戶可以訪問所有三個數據庫,并且只執行常規的多表查詢?這是一個可以接受的解決方案嗎?
While I was writing this question; One idea I got was to have another "super"-user with access to all three databases and just do a regoular multi table query? Would that be an acceptable solution?
如果是這樣,我是否還必須在查詢中指定哪個數據庫?
If so, do I have to specify which database aswell in the query?
推薦答案
您需要一個可以訪問所有三個數據庫的用戶.
You will need a user that has access to all three databases.
您將通過指定完整的表名將它們連接在一起,如下所示:
You will JOIN them together by specifying full table name, something like this:
SELECT * FROM database_1.logs AS d1 LEFT JOIN database_2.users AS d2
ON d1.username = d2.username ORDER BY d1.timestamp DESC LIMIT 10
這篇關于php sql 將來自不同數據庫的多個表連接在一起的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!