【问题标题】:how to get records from one column that is not in another column codeigniter如何从不在另一列codeigniter中的一列中获取记录
【发布时间】:2012-02-09 12:16:45
【问题描述】:

嗨,我正在使用 codeigniter,我有一张这样的表

我想获取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()。或 ids 有任何其他方法。请帮忙 ..... 在此先感谢。

更新

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

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    CodeIgniter 的where_not_in() 无法做到这一点。因为第二个参数应该是一个数组而不是这样的字符串。

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

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

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

    【讨论】:

    • 我将查询更改为 $this->db->where('a.PreferenceID NOT IN (SELECT a.PreferenceParentID FROM '.$table.')', NULL, FALSE);跨度>
    • @KanishkaPanamaldeniya 你能用更新后的代码更新你的帖子吗?
    • 嗯,我犯了一个错误。在我的代码中 $this->db->where('a.PreferenceID NOT IN (SELECT a.PreferenceParentID FROM '.$table.')', NULL, FALSE);它应该是 $this->db->where('a.PreferenceID NOT IN (SELECT PreferenceParentID FROM '.$table.')', NULL, FALSE); . a.PreferenceParentID 应该是 PreferenceParentID
    • 神奇的救生员!谢谢。
    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 2014-03-23
    • 2018-12-08
    相关资源
    最近更新 更多