Php 如何从不在另一列codeigniter中的一列获取记录
嗨,我正在使用codeigniter,我有一张这样的桌子 我想获取Php 如何从不在另一列codeigniter中的一列获取记录,php,mysql,codeigniter,Php,Mysql,Codeigniter,嗨,我正在使用codeigniter,我有一张这样的桌子 我想获取PreferenceID值不在PreferenceParentID列中的所有记录 在本例中,我也使用EntityID对表进行了调整。而且首选项parentId应该是!=0 假设我按entityID53 我的结果应该是 Couture , Denims 因为在这两种情况下,PreferenceID不在PreferenceParentID中。我尝试了where\u not\u in(),但没有成功。请帮忙 这是我的问题 $
PreferenceID
值不在PreferenceParentID
列中的所有记录
在本例中,我也使用EntityID
对表进行了调整。而且首选项parentId
应该是!=0
假设我按entityID
53
我的结果应该是
Couture , Denims
因为在这两种情况下,PreferenceID
不在PreferenceParentID
中。我尝试了where\u not\u in()
,但没有成功。请帮忙
这是我的问题
$table = $this->mastables['shop_profile_preferences'];
$this->db->select('a.ProfilePreferenceID');
$this->db->from($table." as a");
$where2 = "(SELECT a.PreferenceParentID FROM ".$table.")";
$this->db->where_not_in('a.PreferenceID', $where2);
$this->db->where("a.EntityID",$shop_id);
$this->db->where('a.PreferenceParentID !=',0);
$query=$this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
else
{
return FALSE;
}
我的查询结果是
Array
(
[0] => Array
(
[ProfilePreferenceID] => 274
)
[1] => Array
(
[ProfilePreferenceID] => 275
)
[2] => Array
(
[ProfilePreferenceID] => 276
)
)
如何正确使用where\u not\u in()
。或者有其他方法。请帮忙。。。。。
提前谢谢
更新
$table = $this->mastables['shop_profile_preferences'];
$this->db->select('a.ProfilePreferenceID,a.ProfilePreferenceValue');
$this->db->from($table." as a");
$this->db->where('a.PreferenceParentID !=',0);
$this->db->where('a.PreferenceID NOT IN (SELECT a.PreferenceParentID FROM '.$table.')', NULL, FALSE);
$this->db->where("a.EntityID",$shop_id);
$query=$this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
else
{
return FALSE;
}
使用CodeIgniter的
where\u not\u in()
无法做到这一点。因为第二个参数应该是数组,而不是那样的字符串
您可以改为使用通常的where()
方法
$this->db->where('a.PreferenceID NOT IN (SELECT a.PreferenceParentID FROM `table_name`)', NULL, FALSE);
如果您想知道,上面代码中的第三个参数是为了防止CodeIgniter试图用反勾号保护字段和表名。使用CodeIgniter的
where\u not\u In()
无法做到这一点。因为第二个参数应该是数组,而不是那样的字符串
您可以改为使用通常的where()
方法
$this->db->where('a.PreferenceID NOT IN (SELECT a.PreferenceParentID FROM `table_name`)', NULL, FALSE);
如果您想知道,上面代码中的第三个参数是为了防止CodeIgniter试图用反勾号保护字段和表名。我将查询更改为$this->db->where('a.PreferenceID不在(从'$table'中选择a.PreferenceParentID'),NULL,FALSE)@KanishkaPanamaldeniya你能用更新的代码更新你的帖子吗?嗯,我犯了一个错误。在我的代码$this->db->where('a.PreferenceID不在(从“$table.”中选择a.PreferenceParentID),NULL,FALSE);它应该是$this->db->where('a.PreferenceID不在(从“$table.”中选择PreferenceParentID),NULL,FALSE)。a、 PreferenceParentID应该是PreferenceParentID的救命恩人!谢谢。我将查询更改为$this->db->where('a.PreferenceID不在(从'.$table.'中选择a.PreferenceParentID'),NULL,FALSE)@KanishkaPanamaldeniya你能用更新的代码更新你的帖子吗?嗯,我犯了一个错误。在我的代码$this->db->where('a.PreferenceID不在(从“$table.”中选择a.PreferenceParentID),NULL,FALSE);它应该是$this->db->where('a.PreferenceID不在(从“$table.”中选择PreferenceParentID),NULL,FALSE)。a、 PreferenceParentID应该是PreferenceParentID的救命恩人!非常感谢。