vendredi 18 juin 2021

How to merge two rows and get result?

Table Data:

sales_products table data:

product_id quantity sales price
1 4 300
1 5 300
2 3 400
2 2 400
3 3 100

products table

id product_name
1 product_x
2 product_y
3 product_z

Expected Query Output

product_name Quantity Total_Price
product_x 9 2700
product_y 5 2000
product_z 3 300

I was trying with following Query and didn't get expected output

$invoiceDetails = DB::table('products')
    ->join('sales_products', 'sales_products.product_id', '=', 'products.id')
    ->select(
        'products.product_name',
        'sales_products.quantity',
        'sales_products.sales_price',
        DB::raw('(sales_products.quantity * sales_products.sales_price) as total')
    )
    ->where('sales_products.invoice_id', '=', $id)
    ->get();


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire