我需要多对多关系数据库的帮助

I need help for Many to Many relation database

我正在使用 CI 和 hmvc..

我有 3 个 table:

Table 1:

s_id | s_name | s_date

Table 2:

n_id | n_name | n_date

Table 3 包含 table 1 和 table 2 之间的关系:

s_id | n_id

我想通过 ids 获取 table 1 或 table 2 中的值。

一旦我有了 table 2 的 n_id,我想查找并获取 table 3 和 table 1 中的相关值。相关值匹配通过具有相同的 ID。

请帮忙。

我想这可能对你有用。我过去在使用 CI 时做过类似的事情。这是一个应该在您的模型中的 Active Records 查询。这个查询可以缩短,但我让它变得冗长,所以你可以看到发生了什么。

 public function get_ids($n1_id){
    $this->db->select('*');
    $this->db->from('table_3');
    $this->db->join('table_2', 'table_2.n1_id = table_3.n1_id');
    $this->db->join('table_1', 'table.s1_id = table_3.s1_id');
    $this->db->where('n1_id', $n1_id);
    $query = $this->db->get();
    return $query;
}

基本上,一旦您拥有其中一个 ID,就可以将其作为参数传入,并且 select table 3 中与该 ID 匹配的所有行。然后在 table 3 中的匹配行上加入 table 1 和 table 2。

如果可行,请告诉我。我还没有制作 table 来测试它。