获取 Laravel 中两列的最大值和最小值

huangapple go评论71阅读模式
英文:

How to get MAX and MIN of two column in laravel

问题

我有一个类似图片中的表格,一个产品有多个价格和销售记录。我想要获取该产品的最高价格。如果我使用select(\DB::raw("max('price') as price"), 'products.*'),它会返回800,但是这是错误的,因为有一个销售价格是500,所以我需要返回750。我该如何在laravel中实现这个需求?有什么想法吗?

英文:

I have table like in the image, a product with many prices and sales. I want to get max price for that product. If I used select(\DB::raw("max('price') as price"), 'products.*') that will return 800 which is wrong, because it has sale with 500 so I need to return 750. How Can I do this using laravel?
any ideas!?

获取 Laravel 中两列的最大值和最小值

答案1

得分: 1

选择(\DB::raw("MAX(IF(product_detail.sale_price > 0, product_detail.sale_price, product_detail.price)) AS 'max'"))
英文:

Ex: Products has many relationship product_detail
pls try

select(\DB::raw("MAX(IF(product_detail.sale_price > 0, product_detail.sale_price, product_detail.price)) AS 'max'"))

huangapple
  • 本文由 发表于 2023年4月13日 17:31:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定