C
C
cat_crash2012-08-04 18:03:55
MySQL
cat_crash, 2012-08-04 18:03:55

How to make a simple request?

Something to see the heat in the central part strongly "presses" on the brain. It seems like a simple task, but I don’t even know which side to approach ...

There is a table that stores the intervals VALUES and price:

From | Before | Price
0 | 5 | 100
6 | 10 | 200
11| 20 | 300
etc.

The conditions are such that there will be no intersecting gaps in the tables

Task: How to choose a series in which the VALUE will be in the intervals greater than FROM and less than or equal to TO

It sounds simple BUT somehow it does not add up for me

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
ServDev, 2012-08-04
@cat_crash

SELECT `price` FROM  `table` WHERE `from`< 'value' AND `to`>='value' 

get 1 entry if there are no overlapping gaps

V
Vampiro, 2012-08-05
@Vampiro

The first method is also good, but in fact only one boundary can be used for disjoint sets.

CREATE TABLE IF NOT EXISTS `test1` (
  `minVal` int(11) NOT NULL,
  `maxVal` int(11) NOT NULL,
  `price` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`price`),
  UNIQUE KEY `froms` (`minVal`),
  UNIQUE KEY `fr2` (`minVal`,`maxVal`)
) ENGINE=MyISAM;

fill it with values ​​for 100k sets with step 5 (0..4,5..9,…,100000..100005)
EXPLAIN SELECT * 
FROM  `test1` 
WHERE minval <=42000
ORDER BY minval DESC 
LIMIT 1

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE test1 range froms,fr2 fr2 4 NULL 9396 Using where
EXPLAIN SELECT * 
FROM  `test1` 
WHERE minval <=42000
AND maxval >=42000

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE test1 ALL froms,fr2 NULL NULL NULL 20001 Using where
He who has eyes, let him see.

T
Tr1aL, 2012-08-04
@Tr1aL

The most correct would be to use
SELECT a FROM b WHERE c BETWEEN value1 AND value2

L
la0, 2012-08-04
@la0

select * from tbl where VALUE >2 and VALUE <=10
a little more detail please. The text is not very clear what exactly you want to get.

P
Petrusha Ukropov, 2012-08-04
@artishok

SELECT * FROM table WHERE values ​​> FROM AND value <= TO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question