简体   繁体   English

是否可以将表别名附加到列名以找出列的来源?

[英]Is is possible to attach table alias to column names to figure out where columns are coming from?

I have a query that I'm trying to rework that has over 1,000 columns when I select * FROM several tables. 当我从多个表中选择*时,我有一个要重新处理的查询,该查询具有1,000多个列。 I want to know if there is a way in SQL to tag the column alias with the table alias so i can know from which table the columns are from. 我想知道在SQL中是否有一种方法可以用表别名来标记列别名,这样我就可以知道列来自哪个表。 It looks like the following: 看起来如下:

SELECT *
FROM table1 t1
join table2 t2
join table3 t3
join table4 t4

Current column output: id, id, id, id, name, name, name, name, order, order, order, order 当前列输出:id,id,id,id,名称,名称,名称,名称,顺序,顺序,顺序,顺序

Desired Column output: t1.id, t1.name, t1.order, t2.id, t2.name, t2.order,t3.id, t3.name, t3.order, t4.id, t4.name, t4.order 所需的列输出:t1.id,t1.name,t1.order,t2.id,t2.name,t2.order,t3.id,t3.name,t3.order,t4.id,t4.name,t4。订购

this is a very simple example but you can imagine trying to fish out the column you need of a sea of 1,000 columns trying to figure out what table it came from! 这是一个非常简单的示例,但您可以想象一下,尝试找出1000列海中所需的列,以弄清楚它来自哪个表! Any ideas?? 有任何想法吗??

You have to assign non-default column aliases manually: 您必须手动分配非默认列别名:

select t1.id as t1_id, t1.name as t1_name, t1.order as t1_order, 
       t2.id as t2_id, t2.name as t2_name, t2.order as t2_order, 
       . . .

You might find that a spreadsheet or query can help, if you have a lot of columns. 如果您有很多列,您可能会发现电子表格或查询可以提供帮助。

Some products may have exceptions, but generally no, you can't do that. 有些产品可能会有例外,但通常没有,您不能这样做。 You either have to use wildcards ( SELECT * ) or specify the columns you wish returned by full and complete name. 您要么必须使用通配符( SELECT * ),要么以全名和全名指定希望返回的列。

If you specify columns, you can "alias" them, set the column name to something other than the source name. 如果指定列,则可以“别名”它们,将列名称设置为源名称以外的名称。 For example (psuedo-code, leaving out the "ON" clause): 例如(伪代码,省略了“ ON”子句):

SELECT
   T1.Id  as  T1_Id
  ,T2.Id  as  T2_Id
 from table1  T1
 join table2  T2

Note that you can combine table aliases with wildcards. 请注意,您可以将表别名与通配符结合使用。 For example: 例如:

SELECT
   T2.*
 from table1  T1
 join table2  T2
 join table3  T3
 join table4  T5

will return all the columns from table2, and only from table2. 将返回table2中的所有列,并且仅返回table2中的所有列。 This might help in revising your query by getting a list of the available columns in each table. 通过获取每个表中可用列的列表,这可能有助于修订查询。

I'm not aware of a way to prefix each column with the column alias. 我不知道为每列加上列别名的前缀的方法。 However I do know how you could easily break the columns into groups that would allow you to figure out which table each column comes from. 但是,我确实知道如何轻松地将列分为几组,从而使您能够确定每列来自哪个表。

   SELECT 'T1' as [Table1]
   ,  t1.*
   ,  'T2' as [Table2]
   ,  t2.*
   ,  'T3' as [Table3]
   ,  t3.*
   ,  t4.* as [Table4]
   ,  t4.*
   ,  'T5' as [Table5]
   ,  t5.*
   FROM table1 t1
   join table2 t2
   join table3 t3
   join table4 t4

This would break out the columns into groups by table and it would break a little bookmark before and after each group to help you understand where they're coming. 这将按表格将列分为几组,并且会在每个组的前后打破一些书签,以帮助您了解它们的去向。

I know not exactly what you asked for but I believe it would help you a lot in figuring out what's from what tables. 我不完全知道您要的内容,但我相信这对您弄清楚什么表格有很大帮助。

Your other option is as others have said and specifiying the prefix on every column which it sounds like you don't want to do. 正如您所说的那样,您的另一种选择是在听起来像您不想做的每一列上指定前缀。 However it can be a lot quicker to do this if you drag the columns from the Object Explorer - and use ALT-SHIFT to add the prefix to each column. 但是,如果从“对象资源管理器”中拖动列,然后使用ALT-SHIFT将前缀添加到每一列,则可以更快地完成此操作。

Here's an article about copying columns from object explorer - https://www.qumio.com/Blog/Lists/Posts/Post.aspx?ID=56 这是一篇有关从对象资源管理器复制列的文章-https: //www.qumio.com/Blog/Lists/Posts/Post.aspx?ID=56

Her's an article about adjusting code using ALT+SHIFT - https://blogs.msdn.microsoft.com/sql_pfe_blog/2017/04/11/quick-tip-shiftalt-for-multiple-line-edits/ 她的文章关于使用ALT + SHIFT调整代码-https: //blogs.msdn.microsoft.com/sql_pfe_blog/2017/04/11/quick-tip-shiftalt-for-multiple-line-edits/

The first method would take less than a method, the 2nd method I could see taking less than 10 minutes even for 1,000 columns. 第一种方法要比一种方法要少,第二种方法即使对于1,000列,我也可以花不到10分钟的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法找出SQL语法错误的来源 - Can't figure out where SQL Syntax error is coming from 是否可以在 MS Access 2016 数据库的数据透视表中为列名起别名 - Is it possible to alias column names in pivot table in MS Access 2016 database DELETE FROM`table` AS`alias` ... WHERE`alias``column` ...为什么语法错误? - DELETE FROM `table` AS `alias` … WHERE `alias`.`column` … why syntax error? 将行旋转到列名不是来自行的列中 - Rotate rows into columns with column names not coming from the row Perl SQL::Parser 表别名替换:适用于 SELECT 列名但不适用于 WHERE 列名 - Perl SQL::Parser table alias substitution: works for SELECT column names but not for WHERE column names 如何使用Netezza SQL以编程方式找出列名,其中列值与预设值匹配 - How to figure out the column names programmatically using Netezza SQL, where the column values matches a preset value 根据另一个表中的列名从一个表中选择列 - Select columns from a table based on the column names from another table 获取表别名以显示为查询结果中列名的一部分 - Get table alias names to appear as part of the column names in query results 将同一表中的列按GROUP BY划分为两个别名列 - GROUP BY a column in the same table to two alias columns 在表的一列上使用别名的 Where 子句 - Where clause using alias on one column of a table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM