例如,如果我需要加入TableA&表B - 如何使用INFORMATION_SCHEMA数据库查找它们之间的公共字段?
答案 0 :(得分:6)
这是一种方法:
select column_name
from information_schema.columns c
where table_name in ('A', 'B')
group by column_name
having count(*) = 2;
如有必要,还应包括table_schema
以识别表格。
答案 1 :(得分:0)
在两个表中获取公共字段的另一种方法:
SELECT C.name
FROM SYS.OBJECTS O
JOIN SYS.COLUMNS C
ON O.object_id = C.object_id
WHERE O.name IN
(
'TABLE-A', 'TABLE-B'
)
group by C.name
having count(*) = 2;
答案 2 :(得分:0)
获取两个表中的公共列的方法:
SELECT A.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS A
JOIN INFORMATION_SCHEMA.COLUMNS B ON A.COLUMN_NAME = B.COLUMN_NAME
WHERE A.TABLE_NAME 'Table_1'
AND B.TABLE_NAME = 'Table_2'