我的问题是这样的:

$deals=DB::table('leadsheet')
            ->join('Deal', 'leadsheet.leadcode', '=', 'Deal.leadcode')
             ->join('benefits', 'leadsheet.leadcode', '=', 'benefits.leadcode')
             ->join('delegatedealinfo', 'leadsheet.leadcode', '=', 'delegatedealinfo.leadcode')
              ->join('vipbooking', 'leadsheet.leadcode', '=', 'vipbooking.leadcode')
             ->where('id', DB::raw("(select max(`id`) from vipbooking)"))
           ->where('leadsheet.leadcat', '=','Delegates')

            ->get();

所以我有以下表格:
1.leadsheet
   -- leadcode
   -- leadcat
2.Deal
   -- leadcode
3.benefits
   -- leadcode
4.delegatedealinfo
   -- leadcode
5.vipbooking
   -- leadcode

我所要做的是得到所有的最大的ID 表格,其中vipbooking和leadcode > leadcode > leadsheet=cc>=委托。
我唯一的问题是表单不起作用
有人能帮我吗
更新1
使用@anant提供的解决方案后
错误
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from `leadsheet` inner join `Deal` on `leadsheet`.`leadcode` = `Deal`.`leadcode` inner join `benefits` on `leadsheet`.`leadcode` = `benefits`.`leadcode` inner join `delegatedealinfo` on `leadsheet`.`leadcode` = `delegatedealinfo`.`leadcode` inner join `vipbooking` on `leadsheet`.`leadcode` = `vipbooking`.`leadcode` where `id` = (select MAX(id) AS vipid from vipbooking) and `leadsheet`.`leadcat` = Delegates)

改写问题
我想获得代理用户交易的vipbooking表单的详细信息,我们可以从Leadsheet表中找到交易是供应商还是代理
铅板
id | leadcode | leadcat

1  | DL2016012| Delegates
2  | DL2016013| Delegates
3  | VL2016001| Vendors
4  | VL2016002| Vendors

交易
id | leadcode | DealAmount

1  | DL2016012| 123
2  | VL2016002| 1000
2  | DL2016013| 1200

vipbooking公司
    id | leadcode | date      | bookingtxt

    1  | DL2016012| 20-04-2016| xxx
    2  | DL2016012| 20-04-2016| dddd
    3  | VL2016012| 21-04-2016| ppp
    4  | DL2016013| 20-04-2016| xxx
    5  | DL2016013| 22-04-2016| dddd

所以我的输出应该
 2 123 | Delegates| DL2016012| 20-04-2016| dddd
 5 1200| Delegates| DL2016013| 22-04-2016| dddd

谢谢

最佳答案

您可以尝试使用这两个查询。

$vipBookingMaxId = DB::table('vipbooking')
        ->select(DB::raw("MAX(`id`) AS maxId"))
        ->get();
$maxId = (null != $vipBookingMaxId) ? $vipBookingMaxId->maxId : 0;
$dealsQuery = DB::table('leadsheet')
        ->join('Deal', 'leadsheet.leadcode', '=', 'Deal.leadcode')
        ->join('benefits', 'leadsheet.leadcode', '=', 'benefits.leadcode')
        ->join('delegatedealinfo', 'leadsheet.leadcode', '=', 'delegatedealinfo.leadcode')
        ->join('vipbooking', 'leadsheet.leadcode', '=', 'vipbooking.leadcode');

if ($maxId) {
    $dealsQuery->where('id', $vipBookingMaxId->maxId);
}

$deals = $dealsQuery->where('leadsheet.leadcat', '=', 'Delegates')->get();

我希望它能帮助你。

09-05 23:45