1. 程式人生 > >mysql查詢擁有某個欄位的所有表

mysql查詢擁有某個欄位的所有表

前言:最近遇到一個需求,需要給一個數據庫所有的表新增一個欄位,但是一些後建立的表已經有了這個欄位,所以引發了下文。

#查詢指定庫擁有某欄位的表

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%' ) ;

 

希望能夠幫到大家!