Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 检查数据库中是否存在ID_Mysql_Exists - Fatal编程技术网

Mysql 检查数据库中是否存在ID

Mysql 检查数据库中是否存在ID,mysql,exists,Mysql,Exists,当给定一个ID编号时,我想检查它是否存在于数据库中。如果找到ID则返回true,如果未找到,则返回false 我对MySQL的了解非常少,但我认为这可能与COUNT(*)函数有关 select id from table where id = $id 没必要太花哨了。将exists与子查询一起使用似乎只会产生很差的性能,但如果不是这样,我很乐意得到纠正 SELECT ID FROM TABLE WHERE ID = 'number'; SELECT count(*) FROM TABLE W

当给定一个
ID
编号时,我想检查它是否存在于数据库中。如果找到
ID
则返回
true
,如果未找到,则返回
false

我对MySQL的了解非常少,但我认为这可能与
COUNT(*)
函数有关

select id from table where id = $id
没必要太花哨了。将exists与子查询一起使用似乎只会产生很差的性能,但如果不是这样,我很乐意得到纠正

SELECT ID FROM TABLE WHERE ID = 'number'; 
SELECT count(*) FROM TABLE WHERE ID = 'number'; 1 - exists, 

在PHP或其他代码中,必须检查其中一个查询是否返回值。

只需添加另一个示例即可

SELECT COUNT(id) FROM table WHERE id = 123