简体   繁体   English

从一个表中选择 Laravel 5.1 中另一个表中不存在的所有记录

[英]Select all records from one table that do not exist in another table in Laravel 5.1

I want to get all the records from a table which do not exist in other table in Laravel 5.1.我想从 Laravel 5.1 的其他表中不存在的表中获取所有记录。

I know how to do this in core php, and it works fine with the following code我知道如何在核心 php 中执行此操作,并且使用以下代码可以正常工作

SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE t2.name IS NULL

the model该模型

public function audiences() 
{ 
return $this->belongsTo('App\BridalRequest', 'request_id'); 
}

but when i try to do the same in Laravel by using the following code,但是当我尝试使用以下代码在 Laravel 中执行相同操作时,

$all_bridal_requests_check = \DB::table('bridal_requests')
                    ->where(function($query)
                    {
                        $query->where('publisher', '=', 'bq-quotes.sb.com')
                              ->orWhere('publisher', '=', 'bq-wd.com-bsf');
                    })
                    ->whereNotIn('id', function($query) { $query->table('audiences')->select('request_id'); })
                    ->orderBy('created_on', 'desc')
                    ->get();

then it gives me this error然后它给了我这个错误

Call to undefined method Illuminate\\Database\\Query\\Builder::table()调用未定义的方法 Illuminate\\Database\\Query\\Builder::table()

The above mentioned query can be built using laravel query builder in the following manner.可以通过以下方式使用 laravel 查询构建器构建上述查询。

SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE t2.name IS NULL

This is equivalent to below query built using Laravel's query builder.这相当于使用 Laravel 的查询构建器构建的以下查询。

\DB::table('table1 AS t1')
->select('t1.name')
->leftJoin('table2 AS t2','t2.name','=','t1.name')
->whereNull('t2.name')->get();

Late but yes I would like to add into this question.迟到了,但是我想补充这个问题。 Create laravel eloquent relationship between table by writing below code in users model file,通过在users模型文件中编写以下代码,在表之间创建 Laravel eloquent 关系,

public function mu_user()
{
  return $this->hasMany(\App\MainUser::class, 'id');
} 

Now get all records from users table which does not have main user in relation as below,现在从没有主要用户关系的用户表中获取所有记录,如下所示,

$users = Users::doesnthave('mu_user')->get();

If you want to get users who have main_users then you can write query as below,如果你想获得拥有 main_users 的用户,那么你可以编写如下查询,

$users = Author::has('mu_user')->get();

It's such easy.这太容易了。 Hope it helps!希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个表上存在但另一表上不存在的数据中选择 - select from data which exist on one table but do not exist on another table 从一个表中仅选择另一个表中存在的记录 - Selecting only records from one table that exist in another table 使用laravel查询生成器并从一个表中查找另一个表中不存在的记录 - Query Builder and find records from one table which don't exist in another using laravel Laravel - 从一个表中获取记录,该记录在另一个表中不存在,并附有 where 子句 - Laravel - Get records from one table that doesn't exist in another with a where clause attached 如何在cakephp 3.6中从一个表中选择所有记录,并从另一个表中选择一些记录 - how to select all records from one table and some from another table in cakephp 3.6 如何从一个表中选择数据并从另一表中获取所有相关记录? - How to select data from one table and get all associated records from another table? 选择Laravel关系表中不存在的记录 - Select records where not exist in relational table in laravel 从一个表中选择不在另一表中但具有特定条件的记录 - Select records from one table that are not in another table but with specific conditions Laravel 5.1 /雄辩:如何使用第三张表从第二张表中进行选择 - Laravel 5.1 / Eloquent: How do I use a 3rd table to select from the second table Select 全部来自一个表,但如果它存在于另一个表中则删除 - Select all from one table but remove if it exists from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM