Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find if number is between two numbers in different fields in a MySQL database

Tags:

php

mysql

How would you format a query on a MySQL database via PHP to find if an IP address falls between two numbers in two different fields?

Numerical representation of IP address to find:

1265631252

Database format:

 IP FROM      IP TO       REGISTRY  ASSIGNED   CTRY CNTRY COUNTRY
"1265631232","1265893375","arin","1152835200","US","USA","United States"
"1265893376","1265958911","arin","1149120000","US","USA","United States"
"1265958912","1266024447","arin","1149120000","US","USA","United States"
"1266024448","1266089983","arin","1162425600","US","USA","United States"
like image 896
Marcus Avatar asked Mar 02 '10 22:03

Marcus


People also ask

How do I select between values in MySQL?

The MySQL BETWEEN OperatorThe BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included.

Is between query in MySQL?

Overview. Between query in MySQL or BETWEEN is a logical operator is used to select a range of values from the table of the database. Using between in a query, we can also check whether a value is in the provided range or not. BETWEEN operator first validates whether a record lies in the provided range or not.

How do I find the diff in MySQL?

MySQL DATEDIFF() Function The DATEDIFF() function returns the number of days between two date values.

How do I count the same values in a column in MySQL?

Do a SELECT with a GROUP BY clause. Let's say name is the column you want to find duplicates in: SELECT name, COUNT(*) c FROM table GROUP BY name HAVING c > 1; This will return a result with the name value in the first column, and a count of how many times that value appears in the second.


1 Answers

A query like this:

SELECT * FROM your_table WHERE 1265631252 BETWEEN `IP FROM` AND `IP TO`;

Will return the row(s) for which the supplied number is between IP FROM and IP TO.

like image 163
Jordan Running Avatar answered Oct 01 '22 14:10

Jordan Running