簡體   English   中英

如何使用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';

您可以在CodeIgniter中使用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相同的兩列,因此您需要使用u_user.idu_profile.id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM