
- 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
Find minimum unused value in a MySQL table?
You can use LEFT JOIN to find minimum unused value in a MySQL table. Let us first create a table
mysql> create table FindValue -> ( -> SequenceNumber int -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into FindValue values(109); Query OK, 1 row affected (0.14 sec) mysql> insert into FindValue values(110); Query OK, 1 row affected (0.15 sec) mysql> insert into FindValue values(111); Query OK, 1 row affected (0.13 sec) mysql> insert into FindValue values(113); Query OK, 1 row affected (0.13 sec) mysql> insert into FindValue values(114); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from FindValue;
The following is the output
+----------------+ | SequenceNumber | +----------------+ | 109 | | 110 | | 111 | | 113 | | 114 | +----------------+ 5 rows in set (0.00 sec)
Here is the query to find minimum unused value in a MySQL table
mysql> select tbl1 .SequenceNumber+1 AS ValueNotUsedInSequenceNumber -> from FindValue AS tbl1 -> left join FindValue AS tbl2 ON tbl1.SequenceNumber+1 = tbl2.SequenceNumber -> WHERE tbl2.SequenceNumber IS NULL -> ORDER BY tbl1.SequenceNumber LIMIT 1;
The following is the output
+------------------------------+ | ValueNotUsedInSequenceNumber | +------------------------------+ | 112 | +------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Find minimum score from the entire four columns of a table in MySQL
- Getting Minimum and Maximum Value in MySQL
- Insert non-duplicates value in a MySQL table
- Find Unused Fields in Simulink Structure Variables
- How to substring value in a MySQL table column?
- Update a table based on StudentId value in MySQL?
- Setting similar value for a column in a MySQL table?
- Add 30 days to a value in a MySQL table?
- Golang program to find minimum value in a stack
- Find minimum value with MongoDB?
- View the auto_increment value for a table in MySQL without using SHOW TABLE?
- How to find minimum value in MongoDB?
- Check if a value exists in a column in a MySQL table?
- Get the minimum value from a list with multiple columns in MySQL?
- Set value only for NULL values in a MySQL table

Advertisements