前言:最近遇到一個需求,需要給一個數據庫所有的表添加一個字段,但是一些后創建的表已經有了這個字段,所以引發了下文。
*注釋 columnName 字段名 dbName 數據庫名
#查詢指定庫擁有某字段的表
AND TABLE_NAME NOT LIKE 'vw%' 注釋:排除視圖
SELECT DISTINCT TABLE_NAME FROM information_schema.COLUMNS WHERE COLUMN_NAME = 'columnName' AND TABLE_SCHEMA='dbName' AND TABLE_NAME NOT LIKE 'vw%';
#查詢指定數據庫所有的表名
select table_name from information_schema.tables where table_schema='dbName' and table_type='base table';
#查詢指定數據庫沒有某字段的所有表
select table_name from information_schema.tables where table_schema='dbName' and table_type='base table' AND TABLE_NAME NOT IN( SELECT DISTINCT TABLE_NAME FROM information_schema.COLUMNS WHERE COLUMN_NAME = 'culumnName' AND TABLE_SCHEMA='dbName' AND TABLE_NAME NOT LIKE 'vw%' ) ;
希望能夠幫到大家!