如何从CodeIgniter中的一对多关系中选择? [英] How do I do select from a one-to-many relationship in CodeIgniter?

查看:287
本文介绍了如何从CodeIgniter中的一对多关系中选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些表,我想在CodeIgniter访问,其中一个基本上是外键从其他表中的集合,其中大多数只有两个字段:唯一的ID和名称。 广告系列表,让我们称之为,从这些表的id的形式从其他表中提取数据(因为名称可能不是唯一的)。

I have a number of tables that I'm trying to access in CodeIgniter, and one of them is basically a collection of foreign keys from the other tables, most of which are only two fields: a unique id and a name. The "campaigns" table, let's call it, pulls data from other tables in the form of those tables' ids (because the names may not be unique).

我想做的是显示活动数据在人类可读的形式,含义,显示外国名称,而不是ids。我认为这被称为加入,但我不是100%肯定的。我检查了MySql的页面,他们说这不是真正的外键是什么,但我认为你需要外键的加入,所以我真的只是更困惑了。

Now what I want to do is display the "campaigns" data in human-readable form, meaning, showing the foreign names, not the ids. I think this is called a 'join,' but I'm not 100% certain on that. I checked MySql's page and they say that that isn't actually what foreign keys are for, but I thought that you needed foreign keys for a join, so I'm really just more confused now.

我真的使用连接和外键吗?提前感谢。

Do I really use joins and foreign keys for this? Thanks in advance.

推荐答案

外键提供了指向相同或另一个表(通常是后者)中的另一个记录的链接。连接使用两个表(有时是同一个表)中的公共数据来提供它们之间的链接。

The foreign key provides a link to another record in the same or another table (usually the latter). A join uses common data in two tables (sometimes the same table) to provide a link between them.

如果您有两个表要加入,

If you have two tables that you want to join and the only common factor between those two tables is in a third table (the one with all the foreign keys) you will need to perform 2 joins.

示例

SELECT *.table1, *.table2 FROM table1
LEFT JOIN foreign_key_table ON foreign_key_table.id1 = table1.id
LEFT JOIN table2 ON foreign_key_table.id2 = table2.id
WHERE.....

在Codeignter中: / p>

In Codeignter:

$this->db->select('*.table1, *.table2');
$this->db->from('table1');
$this->db->join('foreign_key_table', 'foreign_key_table.id1 = table1.id');
$this->db->join('table2', 'foreign_key_table.id2 = table2.id');
$this->db->where('...');

您可以在这里找到所需的所有信息 - http://codeigniter.com/user_guide/database/active_record.html

You can find all the information you need here - http://codeigniter.com/user_guide/database/active_record.html

希望这有助于。

这篇关于如何从CodeIgniter中的一对多关系中选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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