在不使用“select from"的情况下检查 MySQL 表是否存在;句法?

2021-11-20 00:00:00 sql mysql

有没有一种方法可以检查表是否存在而无需从中选择和检查值?

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.

推荐答案

如果要正确,请使用 INFORMATION_SCHEMA.

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.

相关文章