2010-08-16 123 views
2

我有,我想穿越加入他们更多的表,我要显示的每个表这样的领域:SQL交叉联接查询

tb1.filed1 tb1.filed2 tb2.filed1 .....

我该怎么办?我怎样才能选择字段与细节,如表的名称。

感谢....

+0

对不起 - 我只是不明白你真的在问什么.....你能告诉我们两个表的例子,你在找什么输出? ?也许这会对这个问题提出一些建议。...... – 2010-08-16 13:46:42

+0

请重新说明...正在询问结果中显示的连接或字段名? – Bobby 2010-08-16 13:46:47

回答

1

使用别名来给出一个有意义的描述......例如

select 
    tb1.field1 as "Order ID", 
    tb1.field2 as "Order Date", 
    tb2.field1 as "Product ID" 
    -- ,etc  
from Orders tb1 
inner join OrderProducts tb2 on 
    tb2.OrderID = tb1.OrderID and 
    tb1.OrderID = @OrderID 
+0

@Nix谢谢忘了所有关于格式化......星期一...;) – 2010-08-16 14:15:55

+0

supose,我不知道每个table.I只知道表的名称的字段的名称。 – Farna 2010-08-16 14:38:41

+0

您应该能够生成别名。我假设SQL Server(不知道其他地方的等价物),但下面的查询会得到'订单'表中的所有列: select * from sys.columns where object_id in(select object_id from sys.tables where name ='orders') 你可以看到你如何从返回的名字中建立一个查询。 – 2010-08-16 15:10:17

4

最简单的方法是使用列的别名,以同样的方式,你想给它另名称:

Select 
    tb1.filed1 as 'tb1.filed1', 
    tb1.filed2 as 'tb1.filed2', ... //continue for all your coumns 
From table1 tb1 
Inner Join table2 tb2 on [your criteria] 

但是,我会建议您使用更多的描述性名称。也许类似

Select 
    tb1.filed1 as 'RawInitialFiledDate', 
    tb1.filed2 as 'RawReFileDate', 
    tb2.filed1 as 'ConfirmedInitialFiledDate', 
    tb2.filed2 as 'ConfirmedReFileDate' 
from table1 tb1 
Inner join table2 tb2...