Find integer within +/- 1 from a column in MySQL



For this, use BETWEEN -1 AND 1. Let us first create a table −

mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.76 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(14); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(15); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(16); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(17); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(18); Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+ | Value | +-------+ |    14 | |    15 | |    16 | |    17 | |    18 | +-------+ 5 rows in set (0.00 sec)

Following is the query to find integer within +/- 1 −

mysql> select *from DemoTable    where (Value-17) between -1 and 1;

This will produce the following output −

+-------+ | Value | +-------+ |    16 | |    17 | |    18 | +-------+ 3 rows in set (0.03 sec)
Updated on: 2019-10-10T11:52:23+05:30

155 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements