【问题标题】:Database join query with three tables三张表的数据库连接查询
【发布时间】:2018-03-17 07:41:41
【问题描述】:

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

Drupal 7 数据库选择

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();


}

但是我遇到了一个问题

致命错误:在字符串中调用成员函数 Join() /opt/lampp/htdocs/transgenic/sites/all/modules/report_system/report_system.module 在第 735 行

【问题讨论】:

    标签: php mysql database drupal-7


    【解决方案1】:

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

    连接方法的返回值是被连接的表的别名 分配

    它还指出 -

    连接不能被链接,所以它们必须单独调用(参见 链接)。如果您将多个功能链接在一起,请这样做 这个:

    所以你必须做类似...

    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();
    }
    

    【讨论】:

      【解决方案2】:

      我对drupal不太了解,我找到了这样的解决方案,希望对您有所帮助。

      SelectQuery::join(), SelectQuery::leftJoin() 等不返回查询(它们将别名返回到创建的 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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-02
        相关资源
        最近更新 更多