gpt4 book ai didi

php - 查询生成器 laravel 中的算术运算

转载 作者:可可西里 更新时间:2023-11-01 07:51:05 28 4
gpt4 key购买 nike

我想在 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]

最佳答案

你需要像那样使用原始表达式:

$products =  DB::table('products')
->join('hasDiscount', 'products.idProd', '=', 'hasDiscount.idProd')
->join('discounts', 'discounts.idDiscount', '=', 'hasDiscount.idDiscount')
->select(DB::raw('products.*,(products.price * discounts.tauxDiscount/100) as newPrice'))
->get();

https://laravel.com/docs/5.4/queries#raw-expressions

关于php - 查询生成器 laravel 中的算术运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44367858/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com