2012-07-08 61 views
0

我不知道是否有合併這兩個數據庫查詢的方式,以便有一個:合併這兩個數據庫選擇查詢

$result = $wpdb->get_row("SELECT meta_value FROM ".$wpdb->prefix. 
     "postmeta WHERE meta_key = '_cat_num' AND post_id = $var"); 

$name = $wpdb->get_row("SELECT name FROM ".AH_FEED_DETAILS_TABLE. 
     " WHERE id = " . (int)$result->meta_value); 

return $name->name; 

第一個查詢尋找類別值,然後將其用於查找名稱在AH_FEED_DETAILS_TABLE表字段

回答

2

你可以得到數據庫通過使用與子查詢爲此做如下工作:

$name = $wpdb->get_row("SELECT name FROM ".AH_FEED_DETAILS_TABLE. 
     " WHERE id IN (SELECT meta_value FROM ".$wpdb->prefix. 
     "postmeta WHERE meta_key = '_cat_num' AND post_id = $var)"); 
0
$result = $wpdb->get_row("SELECT name FROM ".$wpdb->prefix 
        .AH_FEED_DETAILS_TABLE." AS cat_tbl JOIN postmeta ON 
        cat_tbl.id=postmeta.meta_value 
        WHERE postmeta.meta_key = '_cat_num' AND postmeta.post_id = $var"); 


return $result->name; 

我希望能幫助給出這個想法