2011-03-10 60 views
2

我試圖選擇沒有名爲'unique'的列的所有表格。我可以選擇有它使用的所有表:在MySQL中選擇不包含特定列的表格

SELECT DISTINCT TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE COLUMN_NAME = 'unique' 
AND TABLE_SCHEMA ='database'; 

有一個簡單的方法來修改SELECT語句,因此列出了沒有該列的所有表?這看起來很簡單,但我無法弄清楚,並且無法在論壇上找到答案。

感謝

回答

5
select 
t.* 
from INFORMATION_SCHEMA.TABLES as t 
    left join INFORMATION_SCHEMA.COLUMNS as c 
    on c.TABLE_NAME = t.TABLE_NAME 
    and c.TABLE_SCHEMA = t.TABLE_SCHEMA 
    and c.COLUMN_NAME = 'unique' 
where c.COLUMN_NAME is null 
and t.TABLE_SCHEMA = 'database' 
+0

非常感謝。這樣可行! – DrewVS 2011-03-10 20:50:18

0
SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME NOT IN (
SELECT DISTINCT TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE COLUMN_NAME = 'unique' 
AND TABLE_SCHEMA ='database') 
+0

我相信這會顯示所有不包含列AND的數據庫中的所有表不在有問題的數據庫中。 – neanderslob 2015-08-26 22:34:14

0

一個簡單的反轉:

SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME NOT IN 

(SELECT DISTINCT TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE COLUMN_NAME = 'unique' 
AND TABLE_SCHEMA ='database'); 
1

上述查詢工作,如果只有一個數據庫服務器中存在。否則,其他數據庫中的表格也將被列出。考慮這個從特定數據庫中挑選表格的簡化版本。總而言之,這是一個偉大的亮點!

SELECT DISTINCT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = 'DATABASE_NAME' 
AND TABLE_NAME NOT IN (SELECT DISTINCT TABLE_NAME 
          FROM INFORMATION_SCHEMA.COLUMNS 
          WHERE COLUMN_NAME = 'FIELD_NAME' 
          AND TABLE_SCHEMA ='DATABASE_NAME');