0

我对 Drupal 7 Mysql 查询有这个问题,尤其是在 LEFT JOIN 上。

我找到了这个解决方案,但我似乎无法将它应用于我的问题,因为我不知道语法是如何进行的。

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) {

}

另外,想知道我成功 LEFT JOIN 后如何检索数据?

如果你能帮助我,我会很高兴。

4

1 回答 1

0

不会想到这会起作用。感谢 rekire 的提示。

$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>";
}
于 2013-02-13T06:31:23.140 回答