获取列不存在的表的名称

时间:2016-06-14 18:35:43

标签: mysql

在MySQL中,我使用什么查询来获取给定列名称尚不存在的表的名称?我有一个数据库,其中一些表格有一个deleted_at列,而另一些表格没有,我希望有一些表格的名称还没有deleted_at列。

为了避免混淆,我不想从一张桌子上检查它是否存在,我想要一张没有列的所有表格列表。谢谢你的帮助!

2 个答案:

答案 0 :(得分:3)

information_schema对这类东西很有用。

SELECT t.table_name 
FROM INFORMATION_SCHEMA.TABLES AS t
LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS c 
   ON t.table_schema = c.table_schema
   AND t.table_name = c.table_name
   AND c.column_name = 'deleted_at'
WHERE t.table_schema = 'my_schema'
   AND c.table_name IS NULL
ORDER BY t.table_name;

答案 1 :(得分:1)

你可以使用这样的信息模式:你必须只有模式(cc)

SELECT * FROM information_schema.Columns
WHERE TABLE_NAME NOT IN (
    SELECT TABLE_NAME FROM information_schema.Columns
    WHERE COLUMN_NAME = 'deleted_at'
    AND TABLE_SCHEMA = 'cc'
    GROUP BY TABLE_NAME
  )
AND TABLE_SCHEMA = 'cc'
GROUP BY TABLE_NAME;

<强>样品

MariaDB [cc]> SELECT * FROM information_schema.Columns
    -> WHERE TABLE_NAME NOT IN (
    ->     SELECT TABLE_NAME FROM information_schema.Columns
    ->     WHERE COLUMN_NAME = 'xid'
    ->     AND TABLE_SCHEMA = 'cc'
    ->     GROUP BY TABLE_NAME
    ->   )
    -> AND TABLE_SCHEMA = 'cc'
    -> GROUP BY TABLE_NAME;
+---------------+--------------+------------+-------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+------------------+------------+----------------+---------------------------------+----------------+
| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | ORDINAL_POSITION | COLUMN_DEFAULT | IS_NULLABLE | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | CHARACTER_OCTET_LENGTH | NUMERIC_PRECISION | NUMERIC_SCALE | DATETIME_PRECISION | CHARACTER_SET_NAME | COLLATION_NAME | COLUMN_TYPE      | COLUMN_KEY | EXTRA          | PRIVILEGES                      | COLUMN_COMMENT |
+---------------+--------------+------------+-------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+------------------+------------+----------------+---------------------------------+----------------+
| def           | cc           | first      | id          |                1 | NULL           | NO          | int       |                     NULL |                   NULL |                10 |             0 |               NULL | NULL               | NULL           | int(11) unsigned | PRI        | auto_increment | select,insert,update,references |                |
+---------------+--------------+------------+-------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+------------------+------------+----------------+---------------------------------+----------------+
1 row in set (0.02 sec)

MariaDB [cc]>