Collections:
INTERVAL() - Interval Position of Sorted List
How to find the interval position of a given number in a sorted number list using the INTERVAL() function?
✍: FYIcenter.com
INTERVAL(x, int1, int2, ...) is a MySQL built-in function that
returns the interval position of x in a given sorted number list.
It uses the following algorithm:
INTERVAL(x, int1, int2, ...intn) = 0, if x < int1 INTERVAL(x, int1, int2, ...intn) = i, if inti <= x < inti+1 INTERVAL(x, int1, int2, ...intn) = n, if intn <= x
For example:
SELECT INTERVAL(22, 23, 30, 44, 200); -- +-------------------------------+ -- | INTERVAL(22, 23, 30, 44, 200) | -- +-------------------------------+ -- | 0 | -- +-------------------------------+ SELECT INTERVAL(10, 1, 10, 100, 1000); -- +--------------------------------+ -- | INTERVAL(10, 1, 10, 100, 1000) | -- +--------------------------------+ -- | 2 | -- +--------------------------------+ SELECT INTERVAL(1000, 1, 10, 100, 1000); -- +----------------------------------+ -- | INTERVAL(1000, 1, 10, 100, 1000) | -- +----------------------------------+ -- | 4 | -- +----------------------------------+
Note that all arguments are treated as numbers (MySQL document incorrectly stated as integers). For example:
SELECT INTERVAL(1000, 1, 10, 100, '1000'); -- +------------------------------------+ -- | INTERVAL(1000, 1, 10, 100, '1000') | -- +------------------------------------+ -- | 4 | -- +------------------------------------+ SELECT INTERVAL(100.6, 1, 10, 100.7, '1000'); -- +---------------------------------------+ -- | INTERVAL(100.6, 1, 10, 100.7, '1000') | -- +---------------------------------------+ -- | 2 | -- +---------------------------------------+ SELECT INTERVAL(100.7, 1, 10, 100.7, '1000'); -- +---------------------------------------+ -- | INTERVAL(100.7, 1, 10, 100.7, '1000') | -- +---------------------------------------+ -- | 3 | -- +---------------------------------------+
Also note that INTERVAL() uses the binary search algorithm. If the value list is not sorted, you may get undesirable result. For example:
SELECT INTERVAL(5.5, 1, 2, 3, 4, 5, 6, 7, 8, 9); -- +------------------------------------------+ -- | INTERVAL(5.5, 1, 2, 3, 4, 5, 6, 7, 8, 9) | -- +------------------------------------------+ -- | 5 | -- +------------------------------------------+ SELECT INTERVAL(5.5, 1, 9, 2, 8, 3, 7, 4, 6, 5); -- +------------------------------------------+ -- | INTERVAL(5.5, 1, 9, 2, 8, 3, 7, 4, 6, 5) | -- +------------------------------------------+ -- | 7 | -- +------------------------------------------+ SELECT INTERVAL(5.5, 1, 9, 2, 3, 8, 7, 4, 6, 5); -- +------------------------------------------+ -- | INTERVAL(5.5, 1, 9, 2, 3, 8, 7, 4, 6, 5) | -- +------------------------------------------+ -- | 4 | -- +------------------------------------------+
Reference information of the INTERVAL() function:
INTERVAL(x, int1, int2, ...): pos Returns the interval position of x in a sorted number list. Arguments, return value and availability: x: Required. The number to be compared. int1, int2, ...: Two or more numbers to be compared against. pos: Return value. The interval position where x should be located. Available since MySQL 4.0.
⇐ EXPORT_SET() - Exporting Binary Set to On/Off Flags
2023-12-19, 1319🔥, 0💬
Popular Posts:
What Are the Differences between DATE and TIMESTAMP in Oracle? The main differences between DATE and...
How to execute statements under given conditions in SQL Server Transact-SQL? How to use IF ... ELSE ...
How To Enter Unicode Character String Literals in SQL Server Transact-SQL? Unicode characters are mu...
How To Set Up SQL*Plus Output Format in Oracle? If you want to practice SQL statements with SQL*Plus...
Where Is the Export Dump File Located in Oracle? If you are not specifying the dump directory and fi...