mardi 7 mai 2019

How can I access the return value in RAW query?

I have a RAW query inside Laravel querybuilder and i will like to add a calculated column using values returned from RAW SELECT statement

See Example:

**
DB::raw("(SELECT SUM(IF(transactions.type='sell', TSL.quantity - TSL.quantity_returned , -1* TPL.quantity) ) 
FROM transactions JOIN transaction_sell_lines AS TSL ON transactions.id=TSL.transaction_id LEFT JOIN 
purchase_lines AS TPL ON transactions.id=TPL.transaction_id
WHERE transactions.status='final' AND transactions.type='sell' $date_pick AND transactions.location_id='$location_id'
AND (TSL.variation_id=v.id OR TPL.variation_id=v.id)) as total_qty_sold"),

 'u.short_name as unit',

DB::raw('SUM((transaction_sell_lines.quantity -
transaction_sell_lines.quantity_returned) * 
transaction_sell_lines.unit_price_inc_tax) as subtotal'))
  ->groupBy('p.id')
  ->make(true);
**

in the above SELECT statement i will like to use the total_qty_sold like

DB:raw(SUM(total_qty_sold * unit) as value)

MYSQL throw error COLUMN NOT FOUND 'total_qty_sold' etc...

Because the raw query requires some conditional statements, and i have multiple in the same single query i can not get require value using join

What can i do to get the column from the raw query.

NB: this is extracted from the one big query.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire