[英]MYSQL: SELECT 1 from table1 where Id = 1 vs SELECT EXISTS(SELECT 1 from table1 where Id = 1)
I want to check whether row exists or not in table1 , Here the condition is on Primary key so want to check which of the query is best to use and why? 我想检查table1中是否存在行,这里的条件是主键,所以想要检查哪个查询最好用,为什么?
QUERY 1: SELECT 1 from table1 where Id = 1;
QUERY 2: SELECT EXISTS(SELECT 1 from table1 where Id = 1)
NOTE: Id is primary key here. 注意:Id是主键。
Thanks!! 谢谢!!
With my MySQL : 用我的MySQL:
IF (SELECT EXISTS(SELECT 1 from table1 where Id = 1)) THEN
-- ...
END ID;
Or without the IF
或没有
IF
SELECT EXISTS(SELECT 1 from table1 where Id = 1) as 'Exists';
+--------+ | Exists | +--------+ | 1 | +--------+ OR +--------+ | Exists | +--------+ | 0 | +--------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.