2012-06-13 82 views
0

基本上我有2級不同的數據庫2個SQL查詢和我試圖比較他們是平等的地方,然後一起加入該值的其他信息。我的第一個查詢包含一個id和一個產品名稱,我的第二個查詢包含一個產品名稱和組件。所以我試圖加入他們的產品名稱,然後顯示其他兩位信息。我選擇的數據庫正在第二個查詢中使用。任何想法我應該做什麼? 到目前爲止,我有這個,這似乎只顯示一個結果:加入兩個MySQL查詢用PHP

$catid = mysql_query("Select a.entry_id, b.cat_name from blog.exp_category_posts a inner join blog.exp_categories b on a.cat_id=b.cat_id where b.Group_ID = 3"); 

$results = mysql_query("Select a.name, c.product from wr_scientific a inner join wr_scientific_products b on a.id=b.scienc_id Join xcart_products c on b.prod_id=c.productid LIMIT 1000"); 

while($row1 = mysql_fetch_array($catid)){ 
$row2= explode("™", $row1['cat_name']); 
$row3= explode("®", $row2[0]); 

while($row = mysql_fetch_array($results)){ 
$rowpro = explode("™", $row['product']); 
$rowprod = explode("®", $rowpro[0]); 

if($rowprod[0] == $row3[0]){ 
echo $rowprod[0].$row['name'].$row1['entry_id']; 
} 
    } 
} 

感謝所有幫助

回答

1

如果這兩個數據庫位於在MySQL(〜同一臺機器)的相同的實例,那麼你可以從(說)db1中通過在表名前面加上數據庫名稱來引用db2中的表格。

E.g:

USE db1 ; 
SELECT 
    db1.table_in_db1.id, -- you can specify the database name here, but 
    table_in_db2.id,  -- there is no ambiguity, the database name is optional 
    field_in_table_in_db2 -- the same way the table name is optionnal when there is no ambiguity 
FROM 
    db1.table_in_db1  -- database name is optionnal here, current database is assumed 
JOIN 
    db2.table_in_db2 
ON ...