2015-10-08 195 views
-3
Table One 

r_c_id r_id Cat_id 
22  34  67 
23  34  68 
24  34  69 
25  34  70 


Table Two 

pc_id pcd_name 
67  abc 
68  AC 
69  dC 
70  aa 

如何獲取名稱和CAT_ID和pcd_name加入MySQL查詢

目前

我使用

SELECT * FROM `rcat` WHERE r_id = '34' 

如何延長我的查詢與加入

+0

這是最基本的連接之一。請顯示你的嘗試。 – Barmar

+0

首先閱讀說明書(您知道關鍵字),然後盡力使用新知識。 (提示:https://dev.mysql.com/doc/refman/5.0/en/join.html) – Pred

+0

public function getProductCategoryById($ id){ $ where = array( 「product_category_id =?」=> $ id ); $ result = $ this - > _ db_table-> fetchRow($ where); // print_r($ result); die; if(!$ result){ return false; } $ product_category = new Application_Model_ProductCategories($ result); return $ product_category; } 如何在其中添加不同的 – user4464505

回答

1
SELECT * 
FROM table1 
INNER JOIN table2 ON table1.r_id = table2.pc_id 
WHERE table1.r_id = '34' 
0
SELECT * FROM table1 a, table2 b WHERE a.cat_id = b.pc_id AND a.r_id = '34'; 
0

即使你沒有提到連接字段,也沒有提及任何f請在兩個表中輸入名稱,但根據數據您的加入應基於cat_id和pc_id,因此您可以嘗試以下查詢 -

SELECT t1.cat_id, t2.pcd_name 
FROM table1 as t1 
JOIN table2 as t2 ON t1.cat_id = t2.pc_id 
WHERE t1.r_id = '34'