从多个表中选择Microsoft SQL Server中的相同列名 [英] Select same column name in microsoft sql server from multiple tables

查看:221
本文介绍了从多个表中选择Microsoft SQL Server中的相同列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写查询:

SELECT * from table1, table 2, table 3 ... 

其中table1.column名称类似于table2.column名称类似于table3.column名称

where table1.column name like table2.column name like table3.column name

我们不知道列的名称,我们说的是表1中的任何列.列名称,例如 table2.列名称,例如表3.列名

We don't know the name of our column, we say any of our columns which are in table1.column name like table2.column name in like table3.column name

例如:employee id可能是3个表所共有的列,或者publisher id是3个表所共有的列.

For example: employee id maybe is a column that is common to 3 tables, or publisher id is a column that is common to 3 tables.

我可以在数据库中找到带有图的外键或主键,但是我想编写一个查询,仅显示标题与列名相同的列.

I can find the foreign key or primary key with a diagram in my database, but I want to write a query that shows just columns which have a same title as column name.

推荐答案

如果您知道列名,并且想知道在哪个不同的表中使用了相同的列名,则编写SQL查询为

If you know the column name and want to know in which different table same column name is used then write SQL query as

SELECT schema_name(t.[schema_id])+'. + t.[名称] AS表来自sys.tables t INNER JOIN sys.columns c on t.[object_id] = c.[object_id] WHERE c.[name] ='Your_Common_Column_Name'

SELECT schema_name(t.[schema_id]) + '.' + t.[name] AS Tables FROM sys.tables t INNER JOIN sys.columns c on t.[object_id] = c.[object_id] WHERE c.[name] = 'Your_Common_Column_Name'

您将获得所有具有相应模式的表列表

You will get all the table list with their respective schema

这篇关于从多个表中选择Microsoft SQL Server中的相同列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆