Codeigniter 中的多重连接 [英] Multiple Joins in Codeigniter

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

问题描述

我是构建数据库的新手,我正在尝试基于具有三个数据库表的 JOIN.

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();

join 函数是这样工作的:join('表名', 'ON 条件', '连接类型');

The join function works like this: 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,测试它,然后转换为活动记录样式可以最大限度地减少错误.

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

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

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