Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.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 NOT IN Condition和Ignited Datatable中的嵌套查询_Php_Sql_Codeigniter - Fatal编程技术网

Php WHERE NOT IN Condition和Ignited Datatable中的嵌套查询

Php WHERE NOT IN Condition和Ignited Datatable中的嵌套查询,php,sql,codeigniter,Php,Sql,Codeigniter,我用的是codeigniter 在我的控制器中,我有如下内容: $this->datatables ->select("customer, sale_status, return_id") ->join('warehouses', 'warehouses.id=sales.warehouse_id', 'left') ->from('sales') ->where('warehouse_id', $warehouse_id)

我用的是codeigniter 在我的控制器中,我有如下内容:

     $this->datatables
    ->select("customer, sale_status, return_id")
    ->join('warehouses', 'warehouses.id=sales.warehouse_id', 'left')
    ->from('sales')
    ->where('warehouse_id', $warehouse_id);          
这很好。但是现在要运行以下查询

    "SELECT * from sales where now() > return_date and id NOT IN (SELECT 
    sale_Id FROM sales where sale_id IS NOT NULL)"

我不知道该怎么说,因为我在他们的文档中没有找到任何“WHERE not in”和嵌套的原始查询语法。任何帮助都将不胜感激,我为我糟糕的英语感到抱歉。

使用codeigniter的正确方法是

$strSubQuery = $this->db->select('sale_Id')->from('sales')->where('sale_id IS NOT NULL', NULL, false)->get_compiled_select();

echo $this->db
        ->select('*')
        ->from('sales')
        ->where('return_date <= ', 'now()', false)
        ->where_not_in('id', $strSubQuery, false)
        ->get_compiled_select();
从文件中:

$this->db->where_not_in()
生成不在('item','item')SQL查询中的WHERE字段,该查询与 如果合适的话

因此,在您的情况下,您可以尝试以下方法:

$now=date('Y-m-d H:i:s');

... 

->select("*")
->from('sales')
->where('return_date <=', $now)
->where_not_in('id',"(SELECT sale_Id FROM sales where sale_id IS NOT NULL)")
$now=date('Y-m-dh:i:s');
... 
->选择(“*”)
->来自(‘销售’)

->where('return_date我想你必须自己将它添加到这个库中是的,我做到了,但我仍然需要嵌套查询的帮助。很抱歉在发布问题后实际解决了第一部分问题。没有太多帮助,它们都没有按预期工作。而我用$now=date('Y-m-d H:i:s')解决了now();但无法在where not in子句中获取sql查询。感谢您的回复。我就是这样做的。我得到的错误是未定义的方法Datatables::get_compiled_select()很抱歉,我无法理解我做错了什么,但是这个get\u compiled\u select方法在ignited datatables类中似乎不存在
get\u compiled\u select
是一个查询生成器方法,它以字符串的形式返回生成的查询-您必须使用查询生成器在我的答案中构建第一个查询,并且这个字符串应该用于您的
where\u not\u in
函数-意思是
$this->datatables->where\u not\u in($strSubQuery,NULL,false)->…
是的,它起作用了。我从$this->datatables->的最后一个子句中删除了get\u compiled\u select方法…谢谢@sintakonte
$now=date('Y-m-d H:i:s');

... 

->select("*")
->from('sales')
->where('return_date <=', $now)
->where_not_in('id',"(SELECT sale_Id FROM sales where sale_id IS NOT NULL)")