tags:

views:

53

answers:

2
+2  Q: 

problem in query

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....

+1  A: 

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
benjynito
@Nix thanks forgot all about formatting... mondays... ;)
benjynito
supose that i don't know the names of fields of every table.i only know the name of tables.
na.fa
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.
benjynito
+4  A: 

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...
AllenG