英文:
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!?
答案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'"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论