我有两张桌子:
bookings(id,lessons_id,user_id,permit_id)
lessons(id,name,date)
我需要选择user_id <>$user
我做了以下但是不太正确。
$user = Auth::id();
$lesson = DB::table('lessons')
->whereNotExists(function ($query) {
$query->select(DB::raw(1))
->from('bookings')
->whereRaw('bookings.lessons_id', '=', 'lessons.id')
->where(' bookings.user_id',' <>', '$user');
})
->get();
答案 0 :(得分:0)
尝试使用whereNotExists
功能:
DB::table('lessons')
->whereNotExists(function ($query) {
$query->select(DB::raw(1))
->from('bookings')
->whereRaw('bookings.lessons_id = lessons.id AND
bookings.user_id = ?', [$user]);
})
->get();
这将对应于以下原始MySQL查询:
SELECT *
FROM lessons t
WHERE NOT EXISTS (SELECT 1 FROM bookings b
WHERE b.lessons_id = t.id AND b.user_id = 2);