三张表的数据库连接查询

Database join query with three tables

我在这里尝试连接三个表,这样我就可以得到我想要的输出

Drupal 7 数据库 select

function abc($Incharge) {
   $res= db_select('node', 'n')
 ->Join('aa', 'f', 'f.id = n.nid')
 ->Join('bb', 'd', 'd.id = f.entity_id');
 return $total_res = $res ->condition('n.type', 'ram')
   ->condition('d.target_id',$Incharge)
   ->condition('n.status', 1)
   ->fields('n', array('nid', 'title'))
   ->orderBy('n.title', 'ASC')
   ->execute()->fetchAllKeyed();


}

但我遇到了一个问题

Fatal error: Call to a member function Join() on string in /opt/lampp/htdocs/transgenic/sites/all/modules/report_system/report_system.module on line 735

我不太了解 drupal 我找到了这样的解决方案希望它能帮助你。

SelectQuery::join(), SelectQuery::leftJoin() 等不 return 查询(它们 return 创建的 JOIN 的别名),所以它们不能链接。

只需像这样分隔代码即可:

$query = db_select('node', 'n')
  ->fields('l')
  ->fields('s', array('stamp', 'message'))
  ->orderBy('`order`', 'ASC');

$query->Join('aa', 'f', 'f.id = n.nid');

$result = $query->execute();

根据文档 (https://www.drupal.org/docs/7/api/database-api/dynamic-queries/joins)

The return value of a join method is the alias of the table that was assigned

它还指出 -

Joins cannot be chained, so they have to be called separately (see Chaining). If you are chaining multiple functions together do it like this:

所以你必须做类似...

function abc($Incharge) {
   $res= db_select('node', 'n');
   $res->Join('aa', 'f', 'f.id = n.nid');
   $res->Join('bb', 'd', 'd.id = f.entity_id');
   return $total_res = $res ->condition('n.type', 'ram')
       ->condition('d.target_id',$Incharge)
       ->condition('n.status', 1)
       ->fields('n', array('nid', 'title'))
       ->orderBy('n.title', 'ASC')
       ->execute()->fetchAllKeyed();
}