如何使用where子句在codeigniter中连接两个表


How to Joing two tables in codeigniter with where clause

请任何一位卡尔帮助我我的查询在之后

where子句中的列"id"不明确

SELECT u_profile.*, u_user.* FROM u_profile JOIN u_user ON u_profile.id = u_user.id WHERE id = '1' AND u_profile.id = '1'

id=1是从哪里来的我不知道请帮我一个。

您需要在where子句中指示表名,因为SQL在指示id所属的表之前不知道它是什么。假设您想同时找到user_id和profile_id:

SELECT u_profile.*, u_user.* FROM u_profile 
JOIN u_user ON u_profile.id = u_user.id 
WHERE u.user_id = '1' AND u_profile.id = '1';

您可以在CodeIgniterFor INNER JOIN:中使用类似的方法

$this->db->select();
$this->db->from('u_profile')->join('u_user', 'u_profile.id = u_user.id');
$this->db->where('u_user.id',1);
$this->db->where('u_profile.id',1);

对于LEFT JOIN,请使用以下命令:

$this->db->select();
$this->db->from('u_profile')->join('u_user', 'u_profile.id = u_user.id','left');
$this->db->where('u_user.id',1);
$this->db->where('u_profile.id',1);

Column 'id' in where clause is ambiguous意味着,两个表中都有两个id相同的列,因此需要使用asu_user.idAndu_profile.id