Laravel Eloquent 在子查询中有两个“WHERE NOT IN”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44743083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:12:42  来源:igfitidea点击:

Laravel Eloquent with two “WHERE NOT IN” in subquery

mysqllaraveleloquent

提问by Wahsei

I have this query that I am having trouble to write query in laravel eloquent ORM.

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

Appreciate if someone can help.

感谢有人可以提供帮助。

Here is SQL Expression:

这是 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)

采纳答案by Sagar Gautam

Try Something like this:

尝试这样的事情:

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

回答by Akshay Kulkarni

Instead of executing 3 different queries you can use like shown below,

您可以使用如下所示的方式,而不是执行 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();

This code will give the exact same query which is asked in the question, to check the query, use following code

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

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

Output will be,

输出将是,

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`)

回答by Wahsei

I corrected the code below pluck('cust') to pluck('cust_name') and pluck('cust_no') to pluck('cust_code') and it works

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

回答by M Khalid Junaid

You could use existsor leftjoin for better performance instead of sub queries on same table like in existing solution, there is no need for these 2 extra sub queries

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

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
)

OR

或者

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