Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP MySQL基于跨多个列的范围进行选择_Php_Mysql_Select_Range - Fatal编程技术网

PHP MySQL基于跨多个列的范围进行选择

PHP MySQL基于跨多个列的范围进行选择,php,mysql,select,range,Php,Mysql,Select,Range,我有如下类似的表格设置: CREATE table TABLE_prices ( PRICE_ID int unsigned not null auto_increment primary key, RANGE1_START int, RANGE1_END int, RANGE1_PRICE decimal(10,2), RANGE2_START int, RANGE2_END int, RANGE2_PRICE decimal(10,2), RANGE3_START int, RANG

我有如下类似的表格设置:

CREATE table TABLE_prices
(
PRICE_ID int unsigned not null auto_increment primary key,
RANGE1_START int,
RANGE1_END int,
RANGE1_PRICE decimal(10,2),
RANGE2_START int,
RANGE2_END int,
RANGE2_PRICE decimal(10,2),    
RANGE3_START int,
RANGE3_END int,
RANGE3_PRICE decimal(10,2)
);
如果以值100为例,是否有方法构建一个select语句来获取基于该值所处范围的价格范围

我的直觉是,我可能不得不从MySQL中提取范围数据,并在PHP中对其进行迭代,但我不知道是否有办法直接使用MySQL


谢谢大家!

试试这个丑陋的解决方案:

SELECT
  PRICE_ID,
  COALESCE(
            CASE WHEN @value BETWEEN RANGE1_START AND RANGE1_END THEN RANGE1_PRICE END,
            CASE WHEN @value BETWEEN RANGE2_START AND RANGE2_END THEN RANGE2_PRICE END,
            CASE WHEN @value BETWEEN RANGE3_START AND RANGE3_END THEN RANGE3_PRICE END
          ) Price
FROM TABLE_prices

我不知道你想要实现什么,但你可以用一句话来表达:

$price = 100;

$sql = "
(
  SELECT 1 as range, price_id, range1_start as range_start, range1_end as range_end FROM table_prices WHERE range1_start >= $price AND range1_end <= $price
) UNION
(
  SELECT 2 as range, price_id, range2_start as range_start, range2_end as range_end FROM table_prices WHERE range2_start >= $price AND range2_end <= $price
) UNION
(
  SELECT 3 as range, price_id, range3_start as range_start, range3_end as range_end FROM table_prices WHERE range3_start >= $price AND range3_end <= $price
)";

如果价格介于范围1和范围2之间呢@彼得斯科夫斯基只是第一个。但我认为这些范围不能相交。没有逻辑,谢谢!我基本上是想根据数量来确定价格。例如,RANGE1\u START=1 RANGE1\u END=10,RANGE2\u START=11 RANGE2\u END=20,等等。因此,如果用户选择数量为15,我需要获得RANGE2\u价格。