我有这样的 SQL 查询,它工作正常:
SELECT A.program_id FROM A
LEFT JOIN B
ON A.program_id = B.program_id
WHERE
B.program_id IS NULL
但我希望它重写成 LARAVEL 风格,像这样:
\DB::table('A')
->join('B', 'A.program_id', '=', 'B.program_id')
->select('A.program_id')
->whereNull('B.program_id')
->get()->toArray();
但是这段代码返回了 0 个结果。
最佳答案
您正在使用join 而不是left join
试试这个
\DB::table('A')
->leftjoin('B', 'A.program_id', '=', 'B.program_id')
->select('A.program_id')
->whereNull('B.program_id')
->where('A.student_id', '=', 5)
->get()->toArray();
关于php - Laravel:左连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44928276/