
- 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
Best way to test if a row exists in a MySQL table
To test whether a row exists in a MySQL table or not, use exists condition. The exists condition can be used with subquery. It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.
For better understanding, firstly we will create a table with the help of CREATE command. The following is the query to create a table −
mysql> CREATE table ExistsRowDemo -> ( -> ExistId int, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.53 sec)
After creating the table successfully, we will insert some records with the help of INSERT command. The query to insert records into the table −
mysql> INSERT into ExistsRowDemo values(100,'John'); Query OK, 1 row affected (0.16 sec) mysql> INSERT into ExistsRowDemo values(101,'Bob'); Query OK, 1 row affected (0.17 sec) mysql> INSERT into ExistsRowDemo values(103,'Carol'); Query OK, 1 row affected (0.20 sec) mysql> INSERT into ExistsRowDemo values(104,'David'); Query OK, 1 row affected (0.13 sec)
After inserting all the records, we can display them with the help of SELECT command, which is as follows −
mysql> SELECT * from ExistsRowDemo;
The following is the output −
+---------+-------+ | ExistId | Name | +---------+-------+ | 100 | John | | 101 | Bob | | 103 | Carol | | 104 | David | +---------+-------+ 4 rows in set (0.00 sec)
We added some records into the table. The syntax to check whether a row exists in a table or not with the help of EXISTS condition is as follows −
SELECT EXISTS(SELECT * FROM yourTableName WHERE yourCondition);
I am applying the above query to get the result −
Note: Firstly, I am considering the condition when row exists in the table. After that, the condition will be mentioned when a row does not exist.
Case 1
In this case, I am giving a condition when row exists. Let us apply the the above syntax to test whether row exists or not.
mysql> SELECT EXISTS(SELECT * from ExistsRowDemo WHERE ExistId=104);
The following is the output −
+------------------------------------------------------+ | EXISTS(SELECT * from ExistsRowDemo WHERE ExistId=104)| +------------------------------------------------------+ | 1 | +------------------------------------------------------+ 1 row in set (0.00 sec)
From the above sample output, it is clear that row exists, since the value we got is 1. This means TRUE!
Case 2
In this case, I am explaining the condition when row does not exist. Applying the above query.
mysql> SELECT EXISTS(SELECT * from ExistsRowDemo WHERE ExistId=105);
The following is the output −
+------------------------------------------------------+ | EXISTS(SELECT * from ExistsRowDemo WHERE ExistId=105)| +------------------------------------------------------+ | 0 | +------------------------------------------------------+ 1 row in set (0.00 sec)
From the above output, we can see the output is 0 i.e. false (row does not exist).
- Related Articles
- Select random row that exists in a MySQL table?
- Best way to update a single column in a MySQL table?
- The best way to check if a file exists using standard C/C++
- How can you test if some record exists or not in a MySQL table using Python?
- Check if a value exists in a column in a MySQL table?
- What is the most efficient way to check the presence of a row in a MySQL table?
- Check if table exists in MySQL and display the warning if it exists?
- Check that a table exists in MySQL?
- INSERT INTO table if a table exists in MySQL else implement CREATE TABLE and create the table
- Check if a table is empty or not in MySQL using EXISTS
- How to check if a table already exists in the database with MySQL with INFORMATION_SCHEMA.TABLES.?
- How to check if a table exists in MySQL and create if it does not already exist?
- How to check if any value is Null in a MySQL table single row?
- What is the best way to show data in a table in Tkinter?
- How to exclude a specific row from a table in MySQL?
