mardi 26 mars 2019

Laravel: How Can I Convert This Sql Statement to Eloquent or Query Builder?

I need to convert products price with different currencies to USD. I have two tables: products and currencies. I have troubles with second JOIN in Laravel

SELECT 
p.slug,
p.price, 
p.currency, 
price * (c2.value_usd * c1.value_usd) as converted_price,
c2.currency as converted_currency

FROM `products` p
JOIN `currencies` c1 ON p.currency = c1.currency
JOIN `currencies` c2 ON c2.currency = 'USD'

I tried this

$query = \DB::table('products')
          ->join('currencies AS c1', 'products.currency', '=', 'c1.currency')
          ->join('currencies AS c2', 'c2.currency', '=', 'USD')      
          ->get();

but got error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'USD' in 'on clause' (SQL: select * from `products` inner join `currencies` as `c1` on `products`.`currency` = `c1`.`currency` inner join `currencies` as `c2` on `c2`.`currency` = `USD`)



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2UcSiH1
via IFTTT

Aucun commentaire:

Enregistrer un commentaire