我在一个数据库中有 2 个表。
例如:
Table 1 Columns:
id | code | name
Table 2 Columns:
id | code | family | etc.
如何根据重叠的代码列查询两个表以检索 family 列?
这是我目前拥有的:
$query = $this->db
->select('*')
->from('table 1')
->where('code', '123');
$query->get()->result();
上述查询将检索代码为 123 的行,但我想从表 2 中获取相应的家庭数据。我该怎么做?
最佳答案
使用 join()。像这样的东西:
$query = $this->db
->select('*')
->from('table1')
->join('table2', 'table1.code = table2.code')
->where('code', '123');
有关该功能的文档在此处:http://ellislab.com/codeigniter/user-guide/database/active_record.html#select
关于php - 如何在 Codeigniter 中查询 2 表数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19392193/