简体   繁体   English

需要获得最接近的最小值和最大值

[英]need to get the nearest minimum and maximum values

Here I have a query regarding the getting nearest minimum and maximum values. 在这里,我有一个关于获取最近的最小值和最大值的查询。 I have a range : 我有一个范围:

2.43 - 3.57 lakhs

Now i need to get the nearest values to minimum and maximum values. 现在,我需要获取最接近最小值和最大值的值。

I have tried with this query didn't get the desired result. 我尝试使用此查询未获得所需的结果。

$othsimlar_prices = "select ABS(exshowroom - ".$price_query['minprice'].") as minprice,
                            ABS(exshowroom - ".$price_query['maxprice'].") as maxprice,
                            make,
                            model 
                    from ncp_variant_cache 
                    order by maxprice";

why you don't use 'where' and 'and' ? 为什么不使用“ where”和“ and”?

$othsimlar_prices = "select * 
                    from ncp_variant_cache
where minprice=ABS(exshowroom - ".$price_query['minprice'].")
and maxprice=ABS(exshowroom - ".$price_query['maxprice'].")
                    order by maxprice"

; ;

OR if you have only one column with price you could just: 或者,如果您只有一栏标价,您可以:

select * from your_table where price between interval1 and interval2 从your_table中选择*,其中间隔1和间隔2之间的价格

Crude way to get it:- 粗略的方法:-

SELECT exshowroom ,
        make,
        model,
        0 AS range_diff
FROM ncp_variant_cache 
WHERE exshowroom BETWEEN ".$price_query['minprice']." AND ".$price_query['maxprice']."
UNION
SELECT exshowroom ,
        make,
        model,
        ABS(exshowroom - ".$price_query['maxprice'].") AS range_diff
FROM ncp_variant_cache 
WHERE exshowroom > ".$price_query['maxprice']."
UNION
SELECT exshowroom ,
        make,
        model,
        ABS(".$price_query['minprice']." - exshowroom) AS range_diff
FROM ncp_variant_cache 
WHERE exshowroom < ".$price_query['minprice']."
ORDER BY range_diff
LIMIT 1

This uses 3 unioned queries. 这使用3个联合查询。 The first gets any record that is within the price range, the 2nd gets the record closest to the max pricewhile the 3rd gets the record closest to the min price. 第一个获取价格范围内的任何记录,第二个获取最接近最高价格的记录,而第三个获取最接近最小价格的记录。 The result is ordered by the difference (with ones within the range having the difference set to 0) limited to 1 row back. 将结果按差(在将差设置为0的范围内的差)限制为1行进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM