连续组合两个相似的条目并输出它们的总和

时间:2017-06-29 08:19:27

标签: mysql

我想在表中获取相同的条目,并将它们的值组合成一个显示的值。我正在使用laravel框架。 这到目前为止做了什么

public function material_approve($id)
{$totalcost = DB::table('tbl_material_requests')
->where('m1number', $id)
->sum(DB::raw('tbl_material_requests.material_quantity * tbl_material_requests.material_cost'));
$material = DB::table('tbl_material_requests')
->where('m1number', $id)
->get();
$m1_no = $id;}

以下是表格迁移

Schema::create('tbl_material_requests', function (Blueprint $table) {
$table->increments('id');
$table->integer('m1number');
$table->string('material_unit');
$table->integer('material_status');
$table->integer('material_quantity');
$table->integer('material_cost');
$table->string('material_name');
$table->timestamps();});

以下是结果的屏幕截图

this is the result showing two entries of tap i want to diplay it as one entry and find its total

提前谢谢

1 个答案:

答案 0 :(得分:0)

也许GROUP BY是您正在寻找的技术:

SELECT material,
       SUM(quantity) as TotQuantity,
       SUM(material_cost) as TotCost
    FROM tbl
    GROUP BY material;

(不,我无法将其翻译成Lavavel;它增加了我不需要的复杂性。)