SQL - Order records by the closest value

后端 未结 2 1126
谎友^
谎友^ 2020-12-22 09:36

I have created one API for product details In this API I also need to attach similar products in response.

So for similar products scenario are like following 1) Pri

2条回答
  •  情歌与酒
    2020-12-22 10:01

    Yes you can perform this operation by using just simple order query as like following

    `Product.where("product.category_id = ?", "category_id").order("abs(products.price - #{your_selected_product_price})")`
    

提交回复
热议问题