soredive
soredive

Reputation: 813

mysql exclude problem

table a
_______________________________
id  col1 col2 col3 ...........col20
1     ............................
2     ............................
3     ............................

table b
_______________________________
id  colA colB colC colD ...... colZ

query
________________________________
select a.*, b.* from a left join b on b.id = a.col20 where a.id = 1;

In this query table a and b has same column name.

And I need both of them.

select a.id as a_id .. b.id as b_id .. from a left join b on b.id = a.col20 where a.id = 1;

How to avoid typing all column name?

Upvotes: 0

Views: 82

Answers (1)

Fosco
Fosco

Reputation: 38506

As far as I know there is not an easy way to select * and exclude columns, and it requires putting the full column list, but I'm sure there are other possibilities.

One way of doing this which would make those a.*, b.* type queries work, but requires some initial setup, is to create a view for the table which aliases all of the columns.

the view of a would be a select query with all of the column names aliased.

create view aview as
select id as a_id,
       col1 as a_col1,
       col2 as a_col2,
...
...
from a

Then anywhere else you could do something like this:

select a.*, b.*
from aview a
left join bview b on b.b_id = a.a_col20
where a.a_id = 1

If the example were that simple and you really only had 2 tables, it would be sufficient to just make a view for one of them.

Hackish, maybe.. I'd probably look to permanently change the column names on the base tables.

Upvotes: 1

Related Questions