我尝试在 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 语句执行此操作时,出现语法问题;
任何有关尝试编写此代码以满足 Laravel 规范的帮助都会有所帮助。
最佳答案
$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();
关于php - 我如何更改此查询以适合 Laravel 的查询生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29953234/