``````goods_id
pay_price``````

## 1 以`goods_id`分组查询

``select sum(pay_price) as one_goods_total_price from order group by goods_id``

## 2 查出所有商品总价格

``select sum(pay_price) as goods__total_price from order ``

## 3 合并到一条查询

``select one_goods__total_price,goods_total_price,CONCAT(ROUND(one_goods__total_price / goods_total_price * 100, 2),'','%') as per from (select sum(pay_price) as one_goods__total_price from order group by goods_id) as one_goods,(select sum(pay_price) as goods_total_price from order) as goods_total``

``````\$subject=DB::table('order')->select(DB::raw("sum(pay_price) as one_goods_total_price")->groupBy('goods_id');

\$subject1=DB::table('order')->select(DB::raw("sum(pay_price) as one_goods_total_price");
\$subject2=DB::query()->select(DB::raw("CONCAT(ROUND(one_goods__total_price / goods_total_price * 100, 2),'','%') as per")->fromRaw("(" .\$ubject->toSql().") as one_goods".",(" .\$subject1->toSql().") as goods_total");

//假如\$subject和\$subject1有其他判断条件，要这样

\$subject2->mergeBindings(\$subSelect)->mergeBindings(\$subSelect2);
//要不where的判断条件会出错
``````