Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ajsmith_codes's avatar

Query all records in table where there isn't a match in the other?

For a one-to-one relationship, how do you query only the records without a match? I see that in MySQL you can't use EXCEPT. I also tried looking at the various joins.

Users Table: id name

Employees Table: user_id

I only want the results from the users table that are not linked to the employees table.

0 likes
3 replies
programators's avatar
Level 17

what about left join :

SELECT u.Id ... FROM users u LEFT JOIN employees e ON u.Id = e.user_id WHERE e.user_id IS NULL

ajsmith_codes's avatar

@programators That worked! Here is my final code:

    $users = DB::table('users')
        ->leftJoin('employees', 'employees.user_id', '=', 'users.id')
        ->where('employees.user_id', '=', null)
        ->get();

Please or to participate in this conversation.