使用两个不同的键连接表mysql


Join tables mysql with two different keys

我需要用不同的键连接相同的表,

我使用的是codeigniter,所以下面是加入它的语法,

    $this->db->join('sc_countries', 'sc_countries.country_id = sc_users.user_country', 'LEFT');
    $this->db->join('sc_countries', 'sc_countries.country_id = sc_agents.agent_country', 'LEFT');

当我尝试以这种方式加入时,它抛出错误

Not unique table/alias: 'sc_countries'

如何进行这种类型的联接?

感谢advnace,

SQL解释器找不到确切的表,因为同一个表被连接了两次。

这是矛盾的。为了避免冲突,请使用usin-alias:重命名表

$this->db->join('sc_countries AS C', 'C.country_id = sc_users.user_country', 'LEFT');
$this->db->join('sc_countries AS D', 'D.country_id = sc_agents.agent_country', 'LEFT');