web-dev-qa-db-fra.com

Comment INNER JOIN 3 tables en utilisant CodeIgniter

Quelqu'un peut-il me dire comment rejoindre 3 table avec php? Exemple

SELECT FROM table1, table2,table on INNERJOIN -------------------

que j’ai un tableau de 3. (tableau de questions, tableau de réponses et tableau de catégories) Voici un exemple de ma page Web.

Time remaining 30 minutes(I will get "30 minutes" form Category table)
1. Question (from question table)
2. answer (from answer table)

Je ne sais pas comment rejoindre 3 table.

8
Wai Yan

ça devrait être comme ça, 

$this->db->select('*');    
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();

selon le cadre d'enregistrement actif CodeIgniters

28
Ket.

.
$this->db->select('*'); $this->db->from('table1'); $this->db->join('table1', 'table1.id = table2.id'); $this->db->join('table1', 'table1.id = table3.id'); $query = $this->db->get();

 

4
Klee

J'ai créé une fonction pour obtenir un tableau avec les valeurs des champs et pour rejoindre. Cela va dans le modèle:

  public function GetDataWhereExtenseJoin($table,$fields,$data) {
    //pega os campos passados para o select
    foreach($fields as $coll => $value){
        $this->db->select($value);
    }
    //pega a tabela
    $this->db->from($table);
    //pega os campos do join
    foreach($data as $coll => $value){
        $this->db->join($coll, $value);
    }
    //obtem os valores
    $query = $this->db->get();
    //retorna o resultado
    return $query->result();

}

Cela va dans le contrôleur:

$data_field = array(
        'NameProduct' => 'product.IdProduct',
        'IdProduct' => 'product.NameProduct',
        'NameCategory' => 'category.NameCategory',
        'IdCategory' => 'category.IdCategory'
        );
    $data_join = array
                    ( 'product' => 'product_category.IdProduct = product.IdProduct',
                      'category' => 'product_category.IdCategory = category.IdCategory',
                      'product' => 'product_category.IdProduct = product.IdProduct'
                    );
    $product_category = $this->mmain->GetDataWhereExtenseJoin('product_category', $data_field, $data_join);

résultat:

echo '<pre>';
    print_r($product_category);
    die;
2
Fabio Souza

Je pense que dans CodeIgniter, il est préférable d’utiliser ActiveRecord comme indiqué ci-dessus. Une dernière chose: vous pouvez utiliser la méthode de chaînage dans AR:

$this->db->select('*')->from('table1')->join('table2','table1.id=table2.id')->...
2
uzsolt

vous pouvez modifier votre code comme ceci 

 $this->db->select('a.nik,b.nama,a.inv,c.cekin,c.cekout,a.tunai,a.nontunai,a.id');
 $this->db->select('DATEDIFF (c.cekout, c.cekin) as lama');
 $this->db->select('(DATEDIFF (c.cekout, c.cekin)*c.total) as tagihan');
 $this->db->from('bayar as a');
 $this->db->join('pelanggan as b', 'a.nik = b.nik');
 $this->db->join('pesankamar_h as c', 'a.inv = c.id');
 $this->db->where('a.user_id',$id);
 $query = $this->db->get();
 return $query->result();

j'espère pouvoir résoudre votre SQL

1
Endang Taryana

Pour exécuter des instructions SQL pures (je ne sais pas à propos de FRAMEWORK- CodeIGNITER !!!) , Vous pouvez utiliser SUB QUERY! La syntaxe serait la suivante

SELECT t1.id FROM exemple t1 INNER JOIN (Sélectionnez l'ID à partir de (exemple2 t1 joint exemple3 t2 sur t1.id = t2.id)) comme t2 ON t1.id = t2.id;

J'espère que vous obtenez mon point! 

1
Tirth Bodawala
function fetch_comments($ticket_id){
    $this->db->select('tbl_tickets_replies.comments, 
           tbl_users.username,tbl_roles.role_name');
    $this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
    $this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
    $this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
    return $this->db->get('tbl_tickets_replies');
}
0
ajo
$this->db->select('*');    
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id','JOIN Type');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();

cela vous donnera les résultats de table1, table2, table3 et vous pouvez utiliser n'importe quel type de jointure dans la troisième variable de la fonction $ this-> db-> join () telle que la fonction intérieure, gauche, droite, etc.

0
Manish
$this->db->select('*');    
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id', 'inner');
$this->db->join('table3', 'table1.id = table3.id', 'inner');
$this->db->where("table1", $id );
$query = $this->db->get();

Où vous pouvez spécifier quel identifiant doit être visualisé ou sélectionné dans un tableau spécifique. Vous pouvez également sélectionner la partie de jointure gauche, droite, extérieure, intérieure, extérieure gauche et extérieure droite sur le troisième paramètre de la méthode de jointure. 

0
lothux1987