开发者

MySQL: Check if there Exist Row(s) Matching a Condition

I have been doing this for quite some time:

SELECT COUNT(*) FROM Table WHERE Condition = *Condition*;

Since I am not interes开发者_开发技巧ted in the total number of rows returned, I wonder if there is a more efficient way to check if there exist any row(s) that match the condition without letting MySQL scan through the entire table.


SELECT CASE
         WHEN EXISTS(SELECT *
                     FROM   YourTable
                     WHERE  Condition = '*Condition*') THEN 1
         ELSE 0
       END  


Try

SELECT COUNT(*) FROM SmallTable
WHERE EXISTS(SELECT * FROM Table WHERE Condition = *Condition*)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜