我需要将具有不同货币的产品价格转换为USD。我有两个表格:产品和货币。
| id | currency | value_usd |
+----+----------+-----------+
| 1 | USD | 1 |
| 2 | AUD | 1.077315 |
| 3 | GBP | 0.620868 |
| 4 | EUR | 0.775338 |
+----+----------+-----------+
+----+-------------+----------+
| id | price | currency |
+----+-------------+----------+
| 1 | 100 | USD |
| 2 | 50 | GBP |
| 3 | 75 | EUR |
| 4 | 60 | GBP |
+----+-------------+----------+
我在Laravel中进行第二次JOIN时遇到问题
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'
我尝试过
$query = \DB::table('products')
->join('currencies AS c1', 'products.currency', '=', 'c1.currency')
->join('currencies AS c2', 'c2.currency', '=', 'USD')
->get();
但出现错误
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`)
答案 0 :(得分:0)
将此DB::raw("('USD')")
放入
加入中
$query = \DB::table('products')
->join('currencies AS c1', 'products.currency', '=', 'c1.currency')
->join('currencies AS c2', 'c2.currency', '=', DB::raw("('USD')"))
->get();
希望它能工作