问题
My query is like this:
$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();
So I have following tables:
1.leadsheet
-- leadcode
-- leadcat
2.Deal
-- leadcode
3.benefits
-- leadcode
4.delegatedealinfo
-- leadcode
5.vipbooking
-- leadcode
What I'm trying to do is to get all the maximum id of vipbooking
form where leadcode
is same as all the leadcode
FROM leadsheet
WHERE leadsheet
.leadcat
=Delegates
My only problem is MAX ID
for vipbooking
form is not working
Can any one one help me out
UPDATE1
after applying the solution provided by @anant
Error
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)
Rephrasing Question
I want to get the details of the vipbooking form for a Deal of a Delegate user , we can find if a Deal is Vendor or Delegate from Leadsheet table
Leadsheet
id | leadcode | leadcat
1 | DL2016012| Delegates
2 | DL2016013| Delegates
3 | VL2016001| Vendors
4 | VL2016002| Vendors
Deals
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
So my Out put should have
2 123 | Delegates| DL2016012| 20-04-2016| dddd
5 1200| Delegates| DL2016013| 22-04-2016| dddd
Thanks
回答1:
You can try with the two queries.
$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();
I hope it will help you.
回答2:
Try this
$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('leadsheet.id', DB::raw("(select max(`id`) from vipbooking)"))
->where('leadsheet.leadcat', '=','Delegates')
->get();
added leadsheet.id
来源:https://stackoverflow.com/questions/35474941/how-to-get-the-max-id-in-a-join-query-in-laravel-5-integrity-constraint-violati