【问题标题】:How to make Join Query model in CodeIgniter如何在 CodeIgniter 中创建 Join Query 模型
【发布时间】:2014-08-27 21:24:13
【问题描述】:

如何在 codeigniter 中编写连接查询...我只想要这样的模型 Select query-

public function getData($col, $table, $where = array())
{
        $this->db->select($col);
        $this->db->from($table);
        $this->db->where($where);
        $query = $this->db->get();
        $result = $query->result();

        return $result;
}

请帮忙

【问题讨论】:

    标签: php codeigniter mysql-workbench


    【解决方案1】:
    $this->db->select('*');
    $this->db->from('blogs');
    $this->db->join('comments', 'comments.id = blogs.id');
    

    http://ellislab.com/codeigniter/user-guide/database/active_record.html#select 请先阅读用户指南,然后再将其发布到 stackoverflow

    【讨论】:

      【解决方案2】:
      join query is there..
      

      试试这个

      $this->db->join('second_table', 'second_table.id = first_table.id');
      

      【讨论】:

      • 我只想要这种格式- public function getData($col, $table, $where = array()) { $this->db->select($col); $this->db->from($table); $this->db->where($where); $query = $this->db->get(); $result = $query->result();返回$结果; }
      • $data['item'] = $this->dbmodel->getData('*','category',array());这将在控制器端。这是用于选择查询。我希望在连接查询中相同。如何编写 ON 条件和所有
      • where 条件下的内容。 where 条件将在 $where 中。对吗?
      【解决方案3】:

      这样试试

      $this->db->select('*');
      $this->db->from('first_table');
      $this->db->join('second_table', 'second_table.col_name = first_table.col_name');
      $query=$this->db->get();
      if($query->num_rows()>0){
      return $query->result_array();
      }
      

      【讨论】:

        【解决方案4】:

        对于连接表,您可以使用 join() 方法。

        $this->db->from(table1)
        $this->db->join('table2','table1.id=table2.table1_id','join options');
        

        on condition 表示您要在哪种条件下加入表格。 加入选项是可选的。 连接选项有:左、右、外、内、左外、右外

        【讨论】:

          猜你喜欢
          • 2013-05-14
          • 1970-01-01
          • 1970-01-01
          • 2013-09-07
          • 2013-12-31
          • 2015-05-25
          • 2013-12-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多