Best way to test if a row exists in a MySQL table

1.3K    Asked by debbieJha in Big Data Hadoop , Asked on Jun 24, 2021

 I'm trying to find out if a row exists in a table. Using MySQL, is it better to do a query like this:

SELECT COUNT(*) AS total FROM table1 WHERE ...

and check to see if the total is non-zero or is it better to do a query like this:

SELECT * FROM table1 WHERE ... LIMIT 1

and check to see if any rows were returned?

In both queries, the WHERE clause uses an index.

Answered by Faith Davidson

 In mysql check if row exists, by using this code:

SELECT EXISTS(SELECT * FROM table1 WHERE Condition)

you can refer to the documentation.

As Per the comment use this code:

SELECT EXISTS(SELECT 1 FROM table1 WHERE Condition)



Your Answer

Interviews

Parent Categories