显示基于id的一条记录

时间:2016-10-27 10:13:58

标签: php mysql laravel

我有两张桌子&travel ;request'表和' travel_details'表

travel_request table
 id  name
  1  aa
  2  bb
 traveling_details table
  id travel_request_id(FK from travel_request)   country
   1      1                                         UK
   2      1                                         CL 
   3      2                                         Australia
   4      2                                         India     

我想列出travel_request_id = 1和travel_request_id = 2的travel_request_id 1记录的一条记录以及travel_request表格详细信息和其他

 $request = DB::table('travel_request')
        ->join('department', 'travel_request.department_id', '=', 'department.id')
        ->join('users', 'travel_request.approver_id', '=', 'users.id')
        ->join('travel_purpose', 'travel_request.travel_purpose_id', '=', 'travel_purpose.id')
        ->join('traveling_details', 'travel_request.id','=','traveling_details.travel_request_id' )
        ->join('country','country.id', '=', 'traveling_details.country_id')
        ->join('travel_request_status','travel_request.status_id', '=', 'travel_request_status.id')
        ->select('travel_request.*', 'travel_request.id as travel_id','department.name as dept_name','users.firstname as approver_name','travel_purpose.purpose','country.name as country_name','traveling_details.from_date','traveling_details.to_date','travel_request_status.status')->where('travel_request.requestor_id',$user_id)->paginate(2);  

此查询显示traveling_details表中的所有记录

2 个答案:

答案 0 :(得分:0)

您可能已经使用任何类型的ORM进行数据库事务处理。尝试在该查询中使用LIMIT 1

答案 1 :(得分:0)

SELECT * FROM `travel_request` 
join `travel_details` ON `travel_details`.`request_id` = `travel_request`.`request_id`
    group by `travel_request`.`request_id`

我创建了一个示例数据库,并以同样的方式完成。您必须明白,当“ON”条件匹配时,内部联接将从表中选择所有行。如果您只想要travel_details中的一行,则可以将GROUP BY用于travel_request.id。这将仅选择匹配条件的第一行。

enter image description here enter image description here enter image description here