我需要能够检测数据库和/或表是否存在于单个查询中,以采取相应的措施。我有这个丑陋的查询工作:
SELECT * FROM
(SELECT COUNT(*) AS `database`
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMATA.SCHEMA_NAME="database_name") AS foo,
(SELECT COUNT(*) AS `table`
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = "database_name"
AND table_name = "table_name") AS bar
该查询返回:
database table
1 0
但是...也许那里有更好的方法。
最佳答案
使用左联接:
SELECT schemata.schema_name AS `database_name`, tables.table_name
FROM INFORMATION_SCHEMA.SCHEMATA schemata
LEFT JOIN INFORMATION_SCHEMA.TABLES tables
ON schemata.schema_name = tables.table_schema
AND tables.table_name = "table_name"
WHERE SCHEMATA.SCHEMA_NAME="database_name"
关于mysql - 检测数据库和表是否存在于同一查询中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16694616/