0

I need to be able to detect if the database and/or the table exists on a single query, to act accordingly. I have this fugly query working:

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

This query returns:

database    table
1           0

But... Maybe there is a better method out there.

4

2 に答える 2

0

これも機能しますが、それがよりきれいかどうかはわかりません:)

SELECT
  MAX(CASE
      WHEN table_schema = 'database_name' THEN 1
      ELSE 0
      END) AS `database`
, MAX(CASE
      WHEN table_schema = 'database_name' AND table_name = 'table' THEN 1
      ELSE 0
      END) AS `table`
FROM information_schema.tables;
于 2013-05-22T14:45:48.280 に答える