views:

122

answers:

3

Hello

I have two tables with a variable amount of columns. (I don't know how many columns or what there names will be) for example Table A and Table B.

TableA:

ID | B_ID | {variable} 

TableB

ID | {variable} 

Query:

SELECT TableA.*, TableB.* FROM TableA INNER JOIN TableB ON TableA.B_ID= TableB.id;

When TableA and TableB both have a column with a same name I can't distinguish between the two different columns. For example of both tables has the column "Name" this query would result in :

ID | ID | B_ID | NAME | NAME |
 1 | 35 | 35   | bob  | jim  |

What I am looking for is a way to differentiate between the two tables. Preferably with a prefex for the column names such as.

TableA_ID | TableB_ID | TableA_B_ID | TableA_NAME | TableB_NAME |
        1 |        35 |          35 |         bob |         jim |

I know of the "AS" keyword but the problem is that I don't know what the column names are going to be before hand. (I don't know if TableA or TableB are going to have the column Name)

So my question is

How do you differentiate the columns between the two tables with a INNER JOIN when the tables may have the same column names ?

I am using SQLite3.

+7  A: 

Your result set (given your query) should have all of the TableA columns followed by all the TableB colums, so when you get to the second ID colum, you know you're into the TableB data.

That said, it is would seem odd to me that you're querying all the data out of two tables about which you know functionally nothing...

AllenG
Sounds like some kind of funky dynamic SQL that might query tables that are generated at run-time by the user.
FrustratedWithFormsDesigner
Agree, if you don't know the column names there is something major wrong with your design.Select statements should never use select *, column names should alwys be specified especially in join where the same column is returned twice, this is wasteful of server resources.
HLGEM
If the tables are being created, they have column names that can be captured then, though...
AllenG
I didn't want to get in to the complexity of the system in my question as its kind of confusing. I have a baseclass that gets a row from TableA, it knows very little about the table design only that it has a FK to TableB. The base class does some simple work (validation, sorting) on the results with the columns it knows about before handing it off to a subclass that knows how to handle some of the other columns where more work is being done. I wanted to keep this in a single query because the device that I am building this system for has an extremely slow file IO. (sec for 10k of data)
Steven smethurst
One big query benchmarks better then 5 smaller queries in testing.
Steven smethurst
@Steven smethurst: In that case, would it make more sense for SQL to do the validation/sorting internally based on a trigger, or with a stored procedure?
AllenG
The subclasses are dynamic (Plugin system) I won't know what they want until they loaded... Although I could quest that info from each of the classes before creating he query, Each subclass could add something to the "WHERE" statement. hmmmm. especially since each subclass will know what fields to include as well. hmmm something to think about.
Steven smethurst
Either way it does not look like there is a way to prefix each column with the table name that it came from. too bad it would have made my life easier.
Steven smethurst
@Steven smethurst - if the subclasses are passing in a "WHERE" clause, have them pass in an array (or IEnumerable) of strings which contain the column names. Then the subclass can even define the order they want the columns.
AllenG
+1  A: 

I don't think there is a way to dynamically name the column aliases in SQL query, but then if you don't know the column names beforehand, what are you using this query for? What values will you use in the application where this query is run?

Sachin Shanbhag
+1  A: 

This is admittedly a hack solution, but this:

SELECT TableA.*, "#", TableB.* FROM TableA INNER JOIN TableB ON TableA.B_ID= TableB.id;

Would produce a list of results which would be divided in two blocks, left and right of the # column.

MPelletier