Laravel 有 2 个条件的 JOIN 查询,其中一个为 NULL

Laravel JOIN query with 2 conditions out of which one is NULL

我需要一个在 OR 中的 2 个条件下连接表的查询。在这 2 个条件中,检查列是否为 IS NULL.

$query->select('users.*', 'o.name', 'r.name')
                     ->join('owners as o', 'users.owner_id', '=', 'o.id')
                     ->join('residents as r', 'users.resident_id', '=', 'r.id');

在上面的查询中我还想检查 users.resident_id IS NULLusers.resident_id = r.id OR users.resident_id IS NULL.

感谢您的宝贵时间和提前帮助。

我还没试过这个..

如何使用 "right join" sql..

来自 w3school.. SQL RIGHT JOIN 关键字。 RIGHT JOIN 关键字 returns 右侧 table (table2) 的所有记录,以及左侧 table (table1) 的匹配记录。当没有匹配时,结果从左侧开始为 NULL。

如果这不起作用,则使用 "whereIn".. select 所有带有空数据的 id,然后 select 来自 join 的所有 id.. 然后你可以使用 whereIn 来 select 带有来自 data1 和 data2 的 id 的所有数据..