简体   繁体   English

在CodeIgniter中联接两个或多个表

[英]Joining two or more tables in CodeIgniter

I'm very new to the concept of joining two tables in CodeIgniter query. 我对在CodeIgniter查询中联接两个表的概念很陌生。 Can someone explain the meaning of each line of these codes, please? 有人可以解释这些代码每一行的含义吗? I don't understand them at all. 我一点都不明白。

    $this->db->select('d.*, u.first_name, u.last_name');          
    $this->db->where('status', -1);        
    $this->db->join('users AS u','u.id = d.user_id');
    $this->db->order_by('d.date','desc');
    return $this->db->get('dtr AS d');

Joins is not a CodeIgniter concept but a relational database concept. Joins不是CodeIgniter概念,而是关系数据库概念。 The SQL code of this query is : 该查询的SQL代码为:

SELECT d.*, u.first_name, u.last_name'
FROM dtr AS d 
INNER JOIN users AS u ON u.id = d.user_id
WHERE status = -1
ORDER BY d.date DESC

the query calls two table and joins them using a common key (user: id and dtr: id). 该查询调用两个表,并使用一个公共键(用户:id和dtr:id)将它们联接。

Just refer to codeigniter user guide here And i suggest if you are not compatible on doing that way try to code like this: 只需参考这里的 codeigniter用户指南,我建议您是否不兼容,请尝试这样的代码:

   $sql = "SELECT * FROM $table_name";
   $this->db->query($sql);

It is just the same way around. 这是相同的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM