Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用where子句连接codeigniter中的两个表_Php_Mysql_Codeigniter_Join_Ambiguous - Fatal编程技术网

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

Php 如何使用where子句连接codeigniter中的两个表,php,mysql,codeigniter,join,ambiguous,Php,Mysql,Codeigniter,Join,Ambiguous,请任何人帮我 我的问题如下 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是什么。 假设要同时查找用户id和配置文件id: SELECT u_pr

请任何人帮我 我的问题如下

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是什么。 假设要同时查找用户id和配置文件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中那样使用内部连接:

$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);
对于左连接,请使用以下命令:

$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);

where子句中的
列“id”是不明确的
意味着,两个表中有两个列的id相同,因此需要使用asu\u user.idu\u profile.id

我的查询遵循$this->db->select('u_profile.*,s_user.*);$this->db->from('u_profile');$this->db->where('u_profile.id',$userId);$this->db->join('u_user','u_user.id=u_profile.id');$result=$this->db->get();您需要在id
WHERE table.id='1'
之前添加
表名
,查询时不带最后一个id,我的意思是
选择u\u profile.*,u\u user.*从u\u profile加入u\u user ON u\u profile.id=u\u user.id,其中u\u profile.id='1'
您的问题已经解决,您还没有回复