将join查询更改为laravel

kd3sttzy  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(334)

我有一个mysql查询

SELECT a.`head_id` AS "Headid",a.`name` AS "HeadName", b.`id` AS "Emp_ID",b.`name` AS "Emp_name" FROM users a join users b ON a.id=b.head_id where a.`head_id`=63 or b.`id`=63

如何将此查询转换为laravel?

$users = DB::table('users a')
            ->join('users b', 'a.id', '=', 'b.head_id')

            ->select('a.head_id as Headid','a.name AS HeadName','b.name AS Emp_name','b.name AS Emp_name')
            ->get();

但不幸的是这不起作用。请帮帮我。

kg7wmglp

kg7wmglp1#

您的查询生成器看起来不错,只需根据您的普通查询添加where过滤器即可

$users = DB::table('users as a')
            ->join('users as b', 'a.id', '=', 'b.head_id')
            ->select('a.head_id as Headid','a.name AS HeadName','b.name AS Emp_name','b.name AS Emp_name')
            ->where('a.head_id', '=', 63)
            ->orWhere('b.id','=', 63)
            ->get();

相关问题