1

我在一个数据库中有 2 个表。

例如:

Table 1 Columns:
id | code | name

Table 2 Columns:
id | code | family | etc.

如何根据重叠代码列查询两个表以检索族列?

这是我目前拥有的:

$query = $this->db
    ->select('*')
    ->from('table 1')
    ->where('code', '123');

$query->get()->result();

上面的查询将检索代码为 123 的行,但我想从表 2 中获取相应的家庭数据。我该怎么做?

4

2 回答 2

4

使用连接()。就像是:

$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

于 2013-10-15T22:33:46.360 回答
0

那么你必须添加一句Join,它会让你查询两个表。

$dataquery = array('table1.code' => '123'); //var in order to where
$this->db->select('table1.id As id1, table2.id As id2') //separate the ids with names
$this->db->join('table2.code','tabla1.code'); //code is overlapping
$query = $this->db->get_where('table1',$dataquery);

return $query->get()->result_array();//Return array if you want

干杯!

于 2013-10-16T02:02:24.883 回答