简体   繁体   English

MYSQL:来自table1的SELECT 1,其中Id = 1 vs SELECT EXISTS(来自table1的SELECT 1,其中Id = 1)

[英]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';

Will show 将会呈现

+--------+
| Exists |
+--------+
|      1 |
+--------+

OR

+--------+
| Exists |
+--------+
|      0 |
+--------+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何 select 来自 table1 的记录,其中 table1.id 存在于 table2 的 id 列中? - How to select the records from a table1 where table1.id exists in id column of table2? 从user1所在的table1中选择*(从名称如'%name%'的用户中选择ID) - select * from table1 where user_id in(select id from users where name like '%name%') SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause 从表1创建表3 AS SELECT WHERE ID =从表2创建ID - Create Table3 AS SELECT WHERE id from table1 = id from table2 从表1,表2中选择*,其中表1.ID = 1,显示具有其他ID的值 - select * from table1, table2 where table1.id = 1 showing values with other id PHP / MySQL-尝试选择两列,其中table1中的id等于table2中的id时出现问题 - PHP/MySQL - Trouble when trying to select two columns where id from table1 is equal to id from table2 在 mysql 中选择 table1 和 table2 中的所有结果,其中两个表都有一个公共 ID - Select all results from table1 as well table2 in mysql where both table has one common id MySQL从表1中选择ID并从表2中选择计数(ID) - Mysql select id from table1 and select count(id) from table2 在Zend中从TABLE1中选择SELECT +(从TABLE1中的SELECT COUNT) - SELECT COUNT + (SELECT COUNT FROM TABLE1 WHERE ) FROM TABLE1 in Zend 如何从表1中选择还没有付款条目的记录? - How to select record from table1 where there is no payment entry yet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM