不存在的 Codeigniter 不起作用

Codeigniter where not exists does not work

我正在处理 Codeigniter 项目并遇到了一个问题。我希望我的模型上的函数能够从 POST table 中获取数据,其中 CATEGORY Table 中的 POST_ID 不为空,而不加入 [=18] =],所以(不带回CATEGORY table中POST_ID不存在的数据)。 所以我尝试了这个方法我怎么能得到这个结果?

code: // MyModel

function get_data() {
    
    
    $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';

    if ($id != null) {
        $this->db->where('ID',$id);
        $result =  $this->db->select('POST.ID,POST.DATE,'
        . ' TYPE.Post_Type P_TYPE,)
        ->join('TYPE', 'POST.TYPE_ID = TYPE.ID', 'left')
        ->order_by('POST.ID', 'DESC')
        ->where_in('POST.Post_Type  = type_1, type_2')
        ->where("NOT EXISTS(SELECT POST_ID FROM CATEGORY WHERE POST.ID=CATEGORY.POST_ID )", FALSE)
        ->get('POST');
    } 
    return $result;
}

感谢您的帮助

如果您尝试让数据不为空(意味着它存在),最好使用 IN,或者如果您正在寻找的答案是相反的,您也可以将其更改为 NOT IN .无论如何,我没有更改您的查询中的任何内容,我只是将 POST_ID 和 IN 放在 where 语句中

function get_data() {

    $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
    if ($id != null) {

        $in = array('type_1', 'type_2');
        $this->db->where('ID',$id);
        $this->db->select('POST.ID,POST.DATE,TYPE.Post_Type as P_TYPE', FALSE);
        $this->db->join('TYPE', 'POST.TYPE_ID = TYPE.ID', 'left');
        $this->db->order_by('POST.ID', 'DESC');
        $this->db->where_in('POST.Post_Type', $in);
        $this->db->where("POST.POST_ID IN(SELECT C.POST_ID FROM CATEGORY C )");
        $this->db->from('POST');
        $result = $this->db->get();
    } 
    return $result;
}