
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the most efficient way to check the presence of a row in a MySQL table?
The most efficient want to check the presence of a row, use the count():
select count(1) from yourTableName where yourCondition;
Let us first create a table:
mysql> create table DemoTable ( Id int, FirstName varchar(20) ); Query OK, 0 rows affected (0.73 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable values(100,'Larry'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(110,'Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(120,'Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(130,'Carol'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(140,'David'); Query OK, 1 row affected (0.12 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output
+------+-----------+ | Id | FirstName | +------+-----------+ | 100 | Larry | | 110 | Sam | | 120 | Mike | | 130 | Carol | | 140 | David | +------+-----------+ 5 rows in set (0.00 sec)
Following is the query to check the presence of a row in a table. Here, we are checking for row with Id = 130:
mysql> select count(1) from DemoTable where Id=130;
This will produce the following output:
+----------+ | count(1) | +----------+ | 1 | +----------+ 1 row in set (0.00 sec)
The above output tells that row is present.
NOTE: If you get 1 that means row is present otherwise the row isn’t present.
- Related Questions & Answers
- What is the most efficient way to select a specific number of random rows in MySQL?
- What is the most efficient way to deep clone an object in JavaScript?
- In MongoDB, what is the most efficient way to get the first and last document?
- What is the most elegant way to check if the string is empty in Python?
- What is the most efficient string concatenation method in python?
- What's the most efficient way to pull data from MySQL so that it is formatted with duplicate values
- What is the way to check the size of all the MySQL databases?
- Best way to test if a row exists in a MySQL table
- What is the fastest way to insert a large number of rows into a MySQL table?
- What's the most efficient way to turn all the keys of an object to lower case - JavaScript?
- What is the most compatible way to install python modules on a Mac?
- Get the average row length of a MySQL table
- What is an efficient way to repeat a string to a certain length in Python?
- Get the second last row of a table in MySQL?
- What is the most efficient way of assigning all my HTML5 video sources as trusted without having to loop over all of them
Advertisements