gpt4 book ai didi

php - 如何从一列中获取不在另一列中的记录 codeigniter

转载 作者:行者123 更新时间:2023-11-29 01:45:09 25 4
gpt4 key购买 nike

您好,我正在使用 codeigniter,我有一个这样的表

enter image description here

我想获取所有 PreferenceID 值不在 PreferenceParentID 列中的记录

在这种情况下,我还使用 EntityID 过滤表。 PreferenceParentID 应该是 != 0

假设我按 entityID 53 过滤

我的结果应该是

Couture , Denims

因为 PreferenceID 在这两种情况下都不在 PreferenceParentID 中。我尝试使用 where_not_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_not_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_not_in() 来做到这一点。因为第二个参数应该是一个数组而不是那样的字符串。

您可以做的是使用通常的 where() 方法。

$this->db->where('a.PreferenceID NOT IN (SELECT a.PreferenceParentID FROM `table_name`)', NULL, FALSE);

如果您想知道,上面代码中的第三个参数是为了防止 CodeIgniter 尝试使用反引号保护字段和表名。

关于php - 如何从一列中获取不在另一列中的记录 codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8848117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com