如何在laravel 5.7中从具有相同条件的两个表中获取数据

时间:2019-06-16 04:12:10

标签: php laravel laravel-5.8

我的数据库中有两个表 1)order_construction_employees 2)order_dismantling_employees

两者的结构都相同

-id

-order_id

-employee_id

我想从这两个表中获取记录,条件为employee_id

示例:从两个employee_id = session('employee_id')

表中获取记录

2 个答案:

答案 0 :(得分:0)

您的sql查询如下:

SELECT OCE.id as OCE_id,OCE.order_id as OCE_order, OCE.employee_id as OCE_eid,
       ODE.id as ODE_id,ODE.order_id as ODE_order, ODE.employee_id as ODE_eid 
FROM order_construction_employees AS OCE 
LEFT JOIN order_dismantling_employees AS ODE 
     ON OCE_eid = ODE_eid
WHERE OCE_eid = session('employee_id')

答案 1 :(得分:0)

$constructionEmployees = DB::table('order_construction_employees')
    ->select('order_construction_employees.id', 'order_construction_employees.order_id', 'order_construction_employees.employee_id');

$employees = DB::table("order_dismantling_employees")
    ->select('order_dismantling_employees.id', 'order_dismantling_employees.order_id', 'order_dismantling_employees.employee_id')
    ->union($constructionEmployees)
    ->where('employee_id', session('employee_id'));
    ->get();

希望这对您有帮助