2

I have more tables that I want to cross join them and I want to show each table with fields like this:

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

What should I do? How can i select fields with details like it's table's name.

thanks....

Raidri
  • 17,258
  • 9
  • 62
  • 65
Farna
  • 1,157
  • 6
  • 26
  • 45
  • I'm sorry - I just don't understand what you're really asking..... can you show us an example of two tables, and what output you're looking for?? Maybe that will shed some light on the issue.... – marc_s Aug 16 '10 at 13:46
  • Please rephrase that...are asking about the join, or the fieldnames which are shown in the result? – Bobby Aug 16 '10 at 13:46

2 Answers2

4

The easiest way is to use column aliasing, the same way you'd give it another name:

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]

I would recommend, however, that you use more decriptive names. Perhaps something like

Select 
  tb1.filed1 as 'RawInitialFiledDate', 
  tb1.filed2 as 'RawReFileDate',
  tb2.filed1 as 'ConfirmedInitialFiledDate', 
  tb2.filed2 as 'ConfirmedReFileDate'
from table1 tb1
Inner join table2 tb2...
Nix
  • 57,072
  • 29
  • 149
  • 198
AllenG
  • 8,112
  • 29
  • 40
1

Use aliases to give a meaningful description... for example

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
Nix
  • 57,072
  • 29
  • 149
  • 198
John Hoven
  • 4,085
  • 2
  • 28
  • 32
  • @Nix thanks forgot all about formatting... mondays... ;) – John Hoven Aug 16 '10 at 14:15
  • supose that i don't know the names of fields of every table.i only know the name of tables. – Farna Aug 16 '10 at 14:38
  • You should be able to generate the aliases. I'm assuming SQL Server (not sure of the equivalents elsewhere) but the following query gets you all of the columns in the 'Orders' table: select * from sys.columns where object_id in (select object_id from sys.tables where name='orders') You can see how you might build a query from the Names returned from that. – John Hoven Aug 16 '10 at 15:10