Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter中的多连接_Php_Database_Codeigniter_Join - Fatal编程技术网

Php Codeigniter中的多连接

Php Codeigniter中的多连接,php,database,codeigniter,join,Php,Database,Codeigniter,Join,我对构建数据库还不熟悉,我正在尝试基于一个有三个数据库表的数据库进行连接 Table A = ID, Name, etc Table B = ID, Name, etc Table C = ID, TableAId, TableBId 我不明白的是如何使用active record进行选择。我正试图尽可能少地提出请求,但在不进行三次单独调用的情况下,如何编写这些请求却让我感到困惑 $this->db->select('*'); $this->db->from('blog

我对构建数据库还不熟悉,我正在尝试基于一个有三个数据库表的数据库进行连接

Table A = ID, Name, etc
Table B = ID, Name, etc
Table C = ID, TableAId, TableBId
我不明白的是如何使用active record进行选择。我正试图尽可能少地提出请求,但在不进行三次单独调用的情况下,如何编写这些请求却让我感到困惑

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$this->db->join('authors', 'authors.id = comments.author_id');
希望你能得到我的榜样

只需添加另一个
$this->db->join()

对于复杂的查询,最好使用ORM,例如

join函数的工作原理如下: 联接('TableName','ON condition','Type of join')

等效sql:

SELECT *
FROM TableA AS A
    INNER JOIN TableC AS C
    ON C.TableAId = A.ID
    INNER JOIN TableB AS B
    ON B.ID = C.ID

我发现,首先编写SQL,测试它,然后转换为活动记录样式可以最大限度地减少错误

如果需要灵活的查询,可以使用:

它使用以下语法
$query=$this->db->query('SELECT*FROM my_table')

以下是查询:

SELECT a.name as namea ,b.name as nameb FROM tablec c
JOIN tablea a ON a.ID = c.ID
JOIN tableb b ON b.ID = c.ID
您可能需要阅读更多有关联接的信息

然后以这样的方式检查您的结果:

$query=$this->db->query(“您的查询”)


使用query命令只是一种快速解决问题的方法,这对他来说并不能解决任何问题。还有潜在的安全风险,因为CI不会转义单个数据。这会产生重复的行。任何解决方案?@JWCMay它永远不会重复行,您有多个匹配项,这就是全部目的。如果需要不同的数据,应重新设计数据库。此答案缺少教育意义上的解释。
SELECT a.name as namea ,b.name as nameb FROM tablec c
JOIN tablea a ON a.ID = c.ID
JOIN tableb b ON b.ID = c.ID
foreach ($query->result_array() as $row)
{
   echo $row['namea'];
   echo $row['nameb'];
}
$this->db->select('*');

$this->db->from('table1');

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

$this->db->join('table3','table2.id=table3.id');

$this->db->join('table4','table3.id=table4.id'); 

$this->db->join('table5','table5.id=table4.id');

$this->db->where('table5.id',$this->session->userdata('id'));//getting value from session and match the id of table5 and then show data

$data=$this->db->get()->result();//all data store in $data variable