以下脚本有什么问题?我试图找到具有以下列名称的所有表:ItemID,QueryID和CurrencyID。目前,它只给我表中有3列中提到的3列,因此其中一列将丢失。
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('ItemID', 'QueryID', 'CurrencyID')
GROUP BY TABLE_NAME
HAVING COUNT(*) = 2;
答案 0 :(得分:3)
如果您希望所有3个匹配以及2个,那么为什么不使用
HAVING COUNT(*) >= 2;
答案 1 :(得分:2)
SELECT t.name
FROM sys.tables t INNER JOIN sys.columns c
ON t.object_id = c.object_id
WHERE c.name IN ('ItemID', 'QueryID', 'CurrencyID')
GROUP BY t.name
HAVING COUNT(DISTINCT c.name) >= 3
答案 2 :(得分:0)
避免>
/ =
/ >=
问题的替代答案:
这只会在找到所有3个时返回一张桌子,但是根据你的问题,wallyk的回答是现场的......如果它适合你,请选择他。
SELECT T.TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES T
WHERE EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS C1 WHERE T.TABLE_NAME=C1.TABLE_NAME AND C1.COLUMN_NAME='ItemID')
AND EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS C2 WHERE T.TABLE_NAME=C2.TABLE_NAME AND C2.COLUMN_NAME='QueryID')
AND EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS C3 WHERE T.TABLE_NAME=C3.TABLE_NAME AND C3.COLUMN_NAME='CurrencyID')