Codeigniter中有多个连接 [英] Multiple Joins in Codeigniter

查看:77
本文介绍了Codeigniter中有多个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

我建立数据库的新用户,我试图基于三个数据库表执行JOIN。 >表A = ID,名称等
表B = ID,名称等
表C = ID,TableAId,TableBId

我无法弄清楚的是使用活动记录如何进行此选择。我试图尽可能少的请求,但是我不知道如何应该这样写,而不进行三个单独的调用。

解决方案

  $ this-> db-> select('*'); 
$ this-> db-> from('TableA AS A'); //我使用别名使连接更容易
$ this-> db-> join('TableC AS C' ,'A.ID = C.TableAId','INNER');
$ this-> db-> join('TableB AS B','B.ID = C.TableBId','INNER');
$ result = $ this-> db-> get();

join函数的工作方式如下:
join('TableName','ON condition' ,'加入类型');



等值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,测试它,然后转换为活动记录样式最小化误差。


I'm new to building databases and I'm trying to do a JOIN based on a having three database tables.

Table A = ID, Name, etc
Table B = ID, Name, etc
Table C = ID, TableAId, TableBId

What I can't figure out is using active record how to make this selection. I'm trying to make as few requests as possible, but am getting stumped on how it should all be written without doing three separate calls.

解决方案

$this->db->select('*');
$this->db->from('TableA AS A');// I use aliasing make joins easier
$this->db->join('TableC AS C', 'A.ID = C.TableAId', 'INNER');
$this->db->join('TableB AS B', 'B.ID = C.TableBId', 'INNER');
$result = $this->db->get();

The join function works like this: join('TableName', 'ON condition', 'Type of join');

The equivilent 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

I found that writing the SQL first, testing it, then converting to the active record style minimizes error.

这篇关于Codeigniter中有多个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆