MySQL query to exclude values having specific last 3 digits

MySQLMySQLi Database

For this, use NOT IN. Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values(1234);
Query OK, 1 row affected (0.54 sec)
mysql> insert into DemoTable values(2345);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(7896);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values(4321);
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Value |
+-------+
| 1234  |
| 2345  |
| 7896  |
| 4321  |
+-------+
4 rows in set (0.00 sec)

Following is the query to exclude values having specific last 3 digits −

mysql> select *from DemoTable where right(Value,3) NOT IN('234','321');

This will produce the following output −

+-------+
| Value |
+-------+
| 2345  |
| 7896  |
+-------+
2 rows in set (0.00 sec)
raja
Published on 03-Sep-2019 11:52:11
Advertisements