如何用laravel编写此语句?
$totalSpend= DB::
select
('
SUM(mt_subscription.total_w_tax)
FROM companies
INNER JOIN mt_client ON companies.id = mt_client.company_id
INNER JOIN mt_subscription on mt_client.client_id = mt_subscription.client_id
WHERE mt_client.company_id = 1
');
错误:
SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有一个错误;请参见语法。检查与您的MariaDB服务器版本相对应的手册以使用正确的语法
答案 0 :(得分:0)
请尝试一下
DB::table('companies AS cm')
->select(DB::raw('SUM(sub.total_w_tax)'))
->join('mt_client AS cl','cm.id','=','cl.company_id')
->join ('mt_subscription AS sub','cl.client_id','=','sub.client_id')
->where('cl.company_id','=','1')->get();
答案 1 :(得分:0)
具有汇总功能时使用DB :: raw
$ totalSpend = DB :: select(DB :: raw(SUM(mt_subscription.total_w_tax))FROM 公司INNER JOIN mt_client ON companies.id = mt_client.company_id INNER JOIN mt_subscription开 mt_client.client_id = mt_subscription.client_id WHERE mt_client.company_id = 1');