
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can I return a record from a table nearest to a user-defined variables value in MySQL?
Let us first create a table −
mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ProductAmount int ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ProductAmount) values(5000); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ProductAmount) values(6000); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(ProductAmount) values(7000); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+---------------+ | CustomerId | ProductAmount | +------------+---------------+ | 1 | 5000 | | 2 | 6000 | | 3 | 7000 | +------------+---------------+ 3 rows in set (0.00 sec)
Following is the query to return the nearest value. Here, we have set a user-defined variable with value 6990. Therefore, now we need to fetch the record nearest to 6990 −
mysql> set @value=6990; Query OK, 0 rows affected (0.00 sec) mysql> select ProductAmount from DemoTable order by ABS(ProductAmount-@value) LIMIT 1;
This will produce the following output −
+---------------+ | ProductAmount | +---------------+ | 7000 | +---------------+ 1 row in set (0.00 sec)
- Related Articles
- Show a MySQL user-defined variables values in the result table?
- Updating a MySQL table row column by appending a value from user defined variable?
- Using User-Defined Variables in MySQL
- User-defined variables vs Local Variables in MySQL?
- System variables vs User-defined variables in MySQL?
- How can we search a record from MySQL table having a date as a value in it?
- Add user defined value to a column in a MySQL query?
- How can we store a value in user-defined variable?
- How can you delete a record from a table using MySQL in Python?
- Increase database field value by specified percentage using user-defined variables in MySQL
- How to display grant defined for a MySQL user?
- How can I update a field in a MySQL database table by adding a value in the second table with a value from the first table?
- How Can we permanently define user-defined variable for a client in MySQL?
- How to delete last record (on condition) from a table in MySQL?
- How to get the second last record from a table in MySQL?

Advertisements