【问题标题】:Laravel Eloquent with two “WHERE NOT IN” in subqueryLaravel Eloquent 在子查询中有两个“WHERE NOT IN”
【发布时间】:2017-11-28 07:28:51
【问题描述】:

我有这个查询,我无法在 laravel eloquent ORM 中编写查询。

感谢有人可以提供帮助。

这是 SQL 表达式:

SELECT DISTINCT cust, cust_no FROM delivery_sap 
WHERE cust NOT IN ( SELECT cust_name FROM customer) 
AND cust_no NOT IN ( SELECT cust_code FROM customer)

【问题讨论】:

标签: mysql laravel eloquent


【解决方案1】:

您可以使用如下所示的方式来代替执行 3 个不同的查询,

DB::table('delivery_sap')
->whereNotIn('cust', function ($query) {
        $query->select('cust_name')->from('customer');
    })
->whereNotIn('cust_no', function ($query) {
        $query->select('cust_code')->from('customer');
    })
->select('cust', 'cust_no')
->distinct('cust')
->get();

此代码将给出与问题中提出的完全相同的查询, 要检查查询,请使用以下代码

DB::table('delivery_sap')
->whereNotIn('cust', function ($query) {
        $query->select('cust_name')->from('customer');
    })
->whereNotIn('cust_no', function ($query) {
        $query->select('cust_code')->from('customer');
    })
->select('cust', 'cust_no')
->distinct('cust')
->toSql();

输出将是,

select distinct `cust`, `cust_no` from `delivery_sap` 
where `cust` not in (select `cust_name` from `customer`) 
and `cust_no` not in (select `cust_code` from `customer`)

【讨论】:

    【解决方案2】:

    您可以使用existsleft 连接以获得更好的性能,而不是像现有解决方案那样在同一张表上进行子查询,不需要这两个额外的子查询

    SELECT DISTINCT cust, cust_no 
    FROM delivery_sap d
    WHERE EXISTS (
        SELECT 1
        FROM delivery_sap
        WHERE cust_name = d.cust OR cust_code = d.cust
    )
    

    SELECT DISTINCT d.cust, d.cust_no 
    FROM delivery_sap d
    LEFT JOIN delivery_sap d1 ON d.cust = d1.cust_name OR d.cust = d1.cust_code 
    WHERE d1.cust IS NULL
    
    DB::table('delivery_sap as d')
        ->leftJoin('delivery_sap as d1', function ($join) {
            $join->on('d.cust','=','d1.cust_name')
                 ->orWhere('d.cust', '=', 'd1.cust_code');
       })
        ->whereNull('d1.cust')
        ->select('cust', 'cust_no')
        ->distinct()
        ->get();
    

    【讨论】:

      【解决方案3】:

      我将 pluck('cust') 下面的代码更正为 pluck('cust_name') 和 pluck('cust_no') 到 pluck('cust_code') 就可以了

      DB::table('delivery_sap')
          ->whereNotIn('cust', DB::table('customer')->pluck('cust_name'))
          ->whereNotIn('cust_no', DB::table('customer')->pluck('cust_code'))
          ->select('cust', 'cust_no')
          ->groupBy('cust', 'cust_no')
          ->get();
      

      【讨论】:

        【解决方案4】:

        试试这样的:

        DB::table('delivery_sap')
            ->whereNotIn('cust', DB::table('customer')->pluck('cust'))
            ->whereNotIn('cust_no', DB::table('customer')->pluck('cust_no'))
            ->select('cust', 'cust_no')
            ->groupBy('cust', 'cust_no')
            ->get();
        

        【讨论】:

        • 不工作。我试图弄清楚为什么。关于在 phpmyadmin 中有效,但我说“'字段列表'中的未知列 'cust'”
        • 您在客户表中有不同的列名。如果您的问题已经解决,请关闭问题
        • 这获得了所需的数据,但并不是问题所要求的。它执行 3 个单独的查询。请参阅下面 Akshay Kulkarni 的回答,了解如何使用高级 wheres 在单个查询中完成此操作
        猜你喜欢
        • 2016-06-13
        • 2014-03-03
        • 1970-01-01
        • 2018-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 1970-01-01
        相关资源
        最近更新 更多