2013-02-13 50 views
0

我有關於Drupal 7 Mysql查詢特別是在左加入這個問題。左加入Drupal 7

我發現這個解決方案,但我似乎無法將它應用於我的問題,因爲我不知道語法如何。

https://drupal.stackexchange.com/questions/4317/how-do-i-write-a-left-join-query

這是我上面的鏈接上找到了解決辦法。

$terms = db_select('taxonomy_index', 'ti') 
    ->fields('ti', array('tid', 'name')) 
    ->leftJoin('taxonomy_term_data', 'ttd', 'ti.tid = ttd.tid') 
    ->condition('vid', 2) 
    ->condition('nid', $nid) 
    ->execute(); 

foreach ($terms as $term) { 
    // $term contains the object for the taxonomy term. 
} 

然而,我如何將它應用到我的查詢有問題。

這是我在MySQL上的LEFT JOIN查詢。

$query = "SELECT sweep_table.end_offer, sweep_table.title, embed.fbp_id, embed.sweep_stat 
    FROM sweep_table, embed 
    WHERE sweep_table.uid=embed.uid AND sweep_table.promo_id=embed.sweep_id"; 

我已經做了第幾行,但其餘的,我不知道如何。

$terms = db_select('sweep_table', 'embed') 
    ->fields('sweep_table', array('end_offer', 'title')) 
    ->fields('embed', array('fbp_id', 'sweep_stat')) 
    ->leftJoin('taxonomy_term_data', 'ttd', 'ti.tid = ttd.tid') //Don't know how to apply to my query. 
    ->condition('vid', 2) 
    ->condition('nid', $nid) 
    ->execute(); 

foreach ($terms as $term) { 

} 

此外,想知道我成功後,如何檢索數據左加入它?

如果你能幫助我,我會很高興。

+1

爲什麼不簡單地執行構建它的SQL語句instat? – rekire 2013-02-13 06:15:17

+0

它工作嗎? 雖然我不知道它是否存在,但是想知道如何檢索數據? 雖然「echo $ term-> sweep_table-> end_offer」不起作用 – 2013-02-13 06:24:42

回答

0

不會認爲這會工作。感謝提示。

$query = "SELECT sweep_table.end_offer, sweep_table.title, embed.fbp_id, embed.sweep_stat FROM sweep_table, embed WHERE sweep_table.uid=embed.uid AND sweep_table.promo_id=embed.sweep_id"; 
$result = db_query($query); 
foreach ($result as $row) { 
    echo $row->end_offer . " " . $row->title . " " . $row->fbp_id . " " . $row->sweep_stat . "<br>"; 
}