如何连接表格中的三行和laravel中的第二个表格

时间:2018-03-12 17:36:13

标签: mysql laravel

在laravel 5.6中我想从连接查询中获取数据库在我的情况下我想加入多个表,如员工,指定,预订,车辆,当我使用join时显示以下错误(SQLSTATE [42000]:语法错误或访问冲突:1066不唯一的表/别名:'employees'(SQL:从bookings选择{来自vehicles内部加入bookingsvehicle_id = vehiclesvehicle_id上的employees内部加入bookingsdriver_id = employeesid内部加入employees bookingsconductor_id = employeesid内部加入employees位于bookingshelper_id = employeesid内心在designations上加入employeesdesg_id = designationsdesc_id))我谷歌几次,但我找不到任何解决方案如何解决这个问题在这种情况下,您的努力将得到提前感谢。

在我的laravel模型预订

public function get_booking(){
        $booking = DB::table('bookings')
            ->join('vehicles', 'bookings.vehicle_id', '=', 'vehicles.vehicle_id')
            ->join('employees', 'bookings.driver_id', '=', 'employees.id')
            ->join('employees', 'bookings.conductor_id', '=', 'employees.id')
            ->join('employees', 'bookings.helper_id', '=', 'employees.id')
            ->join('designations', 'employees.desg_id', '=','designations.desc_id')
            ->get();

        return $booking;

1 个答案:

答案 0 :(得分:0)

您必须使用表别名并加入<a href="#" onclick="fbShareWindowOpen()"> <span id="fbShareButton">Share this on facebook!</span> </a> <script> function fbShareWindowOpen() { var myWindow = window.open("https://www.facebook.com/sharer/sharer.php?u=www.url-to-share.com/", "", "top=30px,width=200,height=100"); } </script> 三次:

designations

您还必须使用列别名(取决于您所需的列):

->join('employees as e1', 'bookings.driver_id', '=', 'e1.id')
->join('employees as e2', 'bookings.conductor_id', '=', 'e2.id')
->join('employees as e3', 'bookings.helper_id', '=', 'e3.id')
->join('designations as d1', 'e1.desg_id', '=','d1.desc_id')    - 
->join('designations as d2', 'e2.desg_id', '=','d2.desc_id')
->join('designations as d3', 'e3.desg_id', '=','d3.desc_id')