【问题标题】:WHERE NOT IN Condition and nested query in Ignited Datatable点燃数据表中的 WHERE NOT IN 条件和嵌套查询
【发布时间】:2018-02-11 18:29:42
【问题描述】:

我正在使用Ignited Datatables 作为代码点火器 在我的控制器中,我有类似的东西:

     $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”和嵌套的原始查询语法。任何帮助将不胜感激,我很抱歉我的英语不好。

【问题讨论】:

  • 我认为你必须自己将它添加到这个库中
  • 是的,我做到了,但我仍然需要嵌套查询的帮助。对于发布问题后实际解决的第一部分感到抱歉。

标签: php sql codeigniter


【解决方案1】:

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();

基本上这意味着您必须将where_not_in 函数添加到您的Ignited DatatablesLibrary

我不知道你是怎么做到的,但可以举个例子

public function where_not_in($key_condition, $val = NULL, $blnEscape = true)
{
    $this->ci->db->where_in($key_condition, $val, $blnEscape);
    return $this;
}

【讨论】:

  • gist.github.com/nirob110/b0f5f3901ff28c734d75f139e3884dc9 我就是这样做的。并且我收到错误未定义方法 Datatables::get_compiled_select() 对不起,但我无法找到我做错的地方,但是这个 get_compiled_select 方法似乎不存在于 ignited datatables 类中
  • get_compiled_select 是一种查询构建器方法,并将生成的查询作为字符串返回 - 您必须使用查询构建器在我的答案中构建第一个查询,并且此字符串应与您的 @987654327 一起使用@函数——意思是$this-&gt;datatables-&gt;where_not_in($strSubQuery, NULL, false)-&gt;...
  • 是的,它奏效了。我从 $this->datatables-> 的最后一个子句中删除了 get_compiled_select 方法...谢谢@sintakonte
【解决方案2】:

来自文档:

$this->db->where_not_in()

生成一个 WHERE 字段 NOT IN ('item', 'item') SQL 查询加入 如果合适的话

here

所以在你的情况下,你可以试试这个:

$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-d H:i:s') 解决了 now();但无法在 where not in 子句工作的地方获得 sql 查询。感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
相关资源
最近更新 更多