我尝试在Laravel中创建此查询,但仍无法使其与查询构建器一起使用。它给了我语法错误但我可以在表中运行它并且运行顺畅。
查询尝试运行:
select A.*, sum(RawAmt) as AmountOwed from (select Login, PatID,
if(length(ApptID) > 0, ApptID, VisitID) as VisitID,
ServiceDate,
TotalCharge,
InsurancePaid,
PrevPaid,
WriteOff,
Refund,
MiscDebit,
AmountOwed as RawAmt,
ApptTime,
ApptDate,
Physician,
isCopay,
HLocation from MDPay_AcctHist where Login='demo') A
group by PatID, VisitID
尝试使用DB :: select和DB :: where中的DB :: raw语句执行此操作时,会出现语法问题;
任何有关尝试编写此内容以满足laravels规范的帮助都会有所帮助。
答案 0 :(得分:1)
$subquery = DB::selectRaw('
Login, PatID,
if(length(ApptID) > 0, ApptID, VisitID) as VisitID,
ServiceDate,
TotalCharge,
InsurancePaid,
PrevPaid,
WriteOff,
Refund,
MiscDebit,
AmountOwed as RawAmt,
ApptTime,
ApptDate,
Physician,
isCopay,
HLocation')
->from('MDPay_AcctHist')
->where('Login', '=', 'demo')
->toSql();
$result = DB::selectRaw('A.*, sum(RawAmt) as AmountOwed')
->from(DB::raw($subquery . ' as A'))
->groupBy('PatID', 'VisitID')
->get();