选择不在另一个表中的所有列laravel 5.5
问题描述:
我有两个表-第一个表称为用户",第二个表称为"buy_courses".
I have two tables - the first one is called 'users' and the second one is called 'buy_courses'.
我正在尝试选择所有用户名不在buy_courses中的用户.我尝试过-
I am trying to select all users those user_name is not in buy_courses. I tried something like -
$users = DB::table('users')
->rightjoin('buy_courses', 'users.user_name', '=', 'buy_courses.user_name')
->get();
当我使用'<>'时,它将返回其user_name位于'buy_courses'中的所有用户,那么我将获得所有用户.什么是正确的查询?
It returns all users, whose user_name is in 'buy_courses', when I am using '<>', then I'm getting all users. What should be the right query?
答
DB::table("users")->select('*')->whereNotIn('user_name',function($query) {
$query->select('user_name')->from('buy_courses');
})->get();
只是 join 实际上是Laravel中的内部联接,因此也许您也可以尝试:
just join actually is inner join in Laravel so actually maybe also you can try:
DB::table('users')
->join('buy_courses', 'users.user_name', '=', 'buy_courses.user_name')
->get();