【问题标题】:How to JOIN three tables in Codeigniter如何在 Codeigniter 中加入三个表
【发布时间】:2014-02-24 02:08:31
【问题描述】:

我正在使用 codeigniter 框架来开发一个音乐 cms。我在 mysql 数据库中有 3 个表,目前我在“专辑”表和“模型,控制器”中工作。我想 SELECT "Album" Table 1 和 JOIN "Album" -> "cat_id" with "Category" -> "cat_id",并获取所有类别记录。

然后我想在“Soundtrack”上加入“Album”->“album_id”->“album_id”,然后获取 A 到 Z 的所有音轨记录。

请有人帮我展示正确的 codeigniter 查询,我如何 SELECT 和 JOIN 表然后从 3 个表中获取记录?

表 1 -> 类别

  • cat_id

  • 猫名

  • cat_title
  • 日期

    表 2 -> 专辑

  • cat_id
  • 专辑编号

  • 专辑标题

  • 专辑详情

    表 3 -> 配乐

  • 专辑编号

  • track_title

  • track_url
  • 日期

【问题讨论】:

标签: php mysql codeigniter join


【解决方案1】:

试试这个数据...

function get_album_data() {
    $this->db->select ( 'album.*,cat.*,s_track.*' )
        ->from ( 'albums as album' )
        ->join ( 'categories cat', 'cat.cat_id = album.cat_id')
        ->join ( 'soundtracks s_tracks ', 's_tracks.album_id = album.album_id');
    $query = $this->db->get ();
    return $query->result ();
}

而对于基准试试这个...

function get_album_datum($album_id) {
    $this->db->select ( 'album.*,cat.*,s_track.*' )
        ->from ( 'albums as album' )
        ->join ( 'categories cat', 'cat.cat_id = album.cat_id')
        ->join ( 'soundtracks s_tracks ', 's_tracks.album_id = album.album_id');
    $this->db->where ( 'album.album_id', $album_id);
    $query = $this->db->get ();
    return $query->row();
}7

855788

【讨论】:

    【解决方案2】:
    public function getdata(){
            $this->db->select('c.country_name as country, s.state_name as state, ct.city_name as city, t.id as id');
            $this->db->from('tblmaster t'); 
            $this->db->join('country c', 't.country=c.country_id');
            $this->db->join('state s', 't.state=s.state_id');
            $this->db->join('city ct', 't.city=ct.city_id');
            $this->db->order_by('t.id','desc');
            $query = $this->db->get();      
            return $query->result();
    }
    

    【讨论】:

    • 欢迎来到 StackOverflow!你能在你的回答中稍微解释一下这段代码吗?
    【解决方案3】:

    尝试如下:

    public function funcname($id)
    {
        $this->db->select('*');
        $this->db->from('Album a'); 
        $this->db->join('Category b', 'b.cat_id=a.cat_id', 'left');
        $this->db->join('Soundtrack c', 'c.album_id=a.album_id', 'left');
        $this->db->where('c.album_id',$id);
        $this->db->order_by('c.track_title','asc');         
        $query = $this->db->get(); 
        return $query->result_array();
    }
    

    如果没有找到结果 CI 返回 false 否则返回 true

    【讨论】:

      【解决方案4】:

      在模型中使用此代码

      public function funcname($id)
      {
          $this->db->select('*');
          $this->db->from('Album a'); 
          $this->db->join('Category b', 'b.cat_id=a.cat_id', 'left');
          $this->db->join('Soundtrack c', 'c.album_id=a.album_id', 'left');
          $this->db->where('c.album_id',$id);
          $this->db->order_by('c.track_title','asc');         
          $query = $this->db->get(); 
          if($query->num_rows() != 0)
          {
              return $query->result_array();
          }
          else
          {
              return false;
          }
      }
      

      【讨论】:

      • 我在哪里传递 $id 我的意思是 album_id ?获取具体记录?
      • 重要的是,如果你有 mytyable.id 和 mysecondtable.id 并且你使用 select(''),第一个 id 字段将不会显示。您需要手动选择('mytable.id as id1, mysecondtable.id as id2, mytable., mysecondtable.*')... codeigniter 连接不包含表前缀,因此记录集不会返回重复字段。
      【解决方案5】:
         Check bellow code it`s working fine and  common model function also 
          supported more then one join and also supported  multiple where condition
       order by ,limit.it`s EASY TO USE and REMOVE CODE REDUNDANCY.
      
         ================================================================
          *Album.php
         //put bellow code in your controller
         =================================================================
      
          $album_id='';//album id 
      
         //pass join table value in bellow format
          $join_str[0]['table'] = 'Category';
          $join_str[0]['join_table_id'] = 'Category.cat_id';
          $join_str[0]['from_table_id'] = 'Album.cat_id';
          $join_str[0]['join_type'] = 'left';
      
          $join_str[1]['table'] = 'Soundtrack';
          $join_str[1]['join_table_id'] = 'Soundtrack.album_id';
          $join_str[1]['from_table_id'] = 'Album.album_id';
          $join_str[1]['join_type'] = 'left';
      
      
          $selected = "Album.*,Category.cat_name,Category.cat_title,Soundtrack.track_title,Soundtrack.track_url";
      
         $albumData= $this->common->select_data_by_condition('Album', array('Soundtrack.album_id' => $album_id), $selected, '', '', '', '', $join_str);
                                     //call common model function
      
          if (!empty($albumData)) {
              print_r($albumData); // print album data
          } 
      
       =========================================================================
         Common.php
          //put bellow code in your common model file
       ========================================================================
      
         function select_data_by_condition($tablename, $condition_array = array(), $data = '*', $sortby = '', $orderby = '', $limit = '', $offset = '', $join_str = array()) {
          $this->db->select($data);
          //if join_str array is not empty then implement the join query
          if (!empty($join_str)) {
              foreach ($join_str as $join) {
                  if ($join['join_type'] == '') {
                      $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id']);
                  } else {
                      $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id'], $join['join_type']);
                  }
              }
          }
      
          //condition array pass to where condition
          $this->db->where($condition_array);
      
      
          //Setting Limit for Paging
          if ($limit != '' && $offset == 0) {
              $this->db->limit($limit);
          } else if ($limit != '' && $offset != 0) {
              $this->db->limit($limit, $offset);
          }
          //order by query
          if ($sortby != '' && $orderby != '') {
              $this->db->order_by($sortby, $orderby);
          }
      
          $query = $this->db->get($tablename);
          //if limit is empty then returns total count
          if ($limit == '') {
              $query->num_rows();
          }
          //if limit is not empty then return result array
          return $query->result_array();
      }
      

      【讨论】:

        【解决方案6】:

        试试这个

        在你的模型中

        如果你想获取所有专辑数据,请使用

          function get_all_album_data() {
        
            $this->db->select ( '*' ); 
            $this->db->from ( 'Album' );
            $this->db->join ( 'Category', 'Category.cat_id = Album.cat_id' , 'left' );
            $this->db->join ( 'Soundtrack', 'Soundtrack.album_id = Album.album_id' , 'left' );
            $query = $this->db->get ();
            return $query->result ();
         }
        

        如果你想获取特定的专辑数据,请使用

          function get_album_data($album_id) {
        
            $this->db->select ( '*' ); 
            $this->db->from ( 'Album' );
            $this->db->join ( 'Category', 'Category.cat_id = Album.cat_id' , 'left' );
            $this->db->join ( 'Soundtrack', 'Soundtrack.album_id = Album.album_id' , 'left' );
            $this->db->where ( 'Album.album_id', $album_id);
            $query = $this->db->get ();
            return $query->result ();
         }
        

        【讨论】:

          猜你喜欢
          • 2018-03-14
          • 2020-11-03
          • 2020-03-02
          • 2016-04-03
          • 1970-01-01
          • 2021-07-10
          • 1970-01-01
          • 2015-10-26
          • 1970-01-01
          相关资源
          最近更新 更多