本文介绍了在不使用“从...中选择"的情况下检查表是否存在.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种方法可以检查表是否存在而无需从中选择和检查值?
Is there a way to check if a table exists without selecting and checking values from it?
也就是说,我知道我可以去SELECT testcol FROM testtable
并检查返回的字段数,但是似乎必须有一种更直接/更优雅的方法.
That is, I know I can go SELECT testcol FROM testtable
and check the count of fields returned, but it seems there must be a more direct / elegant way to do it.
推荐答案
您无需计数.
SELECT 1 FROM testtable LIMIT 1;
如果没有错误,则表存在.
If there's no error, table exists.
或者,如果您想正确,请使用 INFORMATION_SCHEMA .
Or, if you want to be correct, use INFORMATION_SCHEMA.
SELECT *
FROM information_schema.tables
WHERE table_schema = 'yourdb'
AND table_name = 'testtable'
LIMIT 1;
或者,您可以使用SHOW TABLES
SHOW TABLES LIKE 'yourtable';
如果结果集中有一行,则表存在.
If there is a row in the resultset, table exists.
这篇关于在不使用“从...中选择"的情况下检查表是否存在.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!