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

后端 未结 12 2255
小蘑菇
小蘑菇 2020-11-22 06:11

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 ...
         


        
12条回答
  •  眼角桃花
    2020-11-22 06:47

    You could also try EXISTS:

    SELECT EXISTS(SELECT * FROM table1 WHERE ...)
    

    and per the documentation, you can SELECT anything.

    Traditionally, an EXISTS subquery starts with SELECT *, but it could begin with SELECT 5 or SELECT column1 or anything at all. MySQL ignores the SELECT list in such a subquery, so it makes no difference.

提交回复
热议问题