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*)
精彩评论