我想prerform在laravel的查询生成器此查询5.4算术运算
select title, price, price*tauxDiscount/100 as newPrice
from products p, hasdiscount pd, discounts d
WHERE p.idProd = pd.idProd
and d.idDiscount = pd.idDiscount
and now() BETWEEN dateStart and dateEnd
,所以我写这篇
$products = DB::table('products')
->join('hasDiscount', 'products.idProd', '=', 'hasDiscount.idProd')
->join('discounts', 'discounts.idDiscount', '=', 'hasDiscount.idDiscount')
->select('products.*', '(products.price * discounts.tauxDiscount/100) as newPrice')
->get();
,但他表现出的这种错误
[SQLSTATE[42S22]: Column not found: 1054 Unknown column '(products.price
* discounts.tauxDiscount/100)' in 'field list' (SQL: select
`products`.*, `(products`.`price * discounts`.`tauxDiscount/100)` as
`newPrice` from `products` inner join `hasDiscount` on
`products`.`idProd` = `hasDiscount`.`idProd` inner join `discounts` on
`discounts`.`idDiscount` = `hasDiscount`.`idDiscount`)][1]
使用DBRaw()来定义计算列 –
感谢@MarkBaker –