Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have 3 tables (mysql db) on which I want to do an inner join. The problem is that these tables have common columns. I know I could use aliases to differentiate them (select A.x as Ax, B.x as Bx, C.c as Cx ...) but this requires to list the columns. However it is not really an option. Is there a way I could give an alias to all the columns of a table at once?

Something like select a.* as a*?

share|improve this question
2  
You should always give the columns list regardless. But yes, a.* should work (without a possibility for column aliases), and you could as well try it yourself... –  dezso Feb 21 at 10:36
    
Yes thanks, I know it works and I did try it myself, but you are not answering my question. –  Chris Feb 25 at 6:58
    
This is why I left it in a comment and not an answer. However, I would like to know why listing the columns is not an option... –  dezso Feb 25 at 9:11
    
Because it is kind of an ORM, that we want as agnostic to the content as possible. But anyway, I got around by using introspection on my objects. Thanks! –  Chris Feb 26 at 10:18
    
One approach can be to add prefixes to the column, derived from the table name, like a first name column on a table calles order, gets the name o_first_name, for example. If you manage to keep the prefixes unique, it can solve such a problem. –  dezso Feb 26 at 10:45

2 Answers 2

You need to specify every column by your self.

Example : Table Country and City both have a name column

SELECT 
    a.Name AS CountryNane, b.Name AS CityName, c.*
FROM
    Country a
        JOIN
    City b ON a.Code = b.CountryCode
        JOIN
    CountryLanguage c ON a.Code = c.CountryCode;
share|improve this answer

If you want to assign an alias to a field you must explicit that field in the list fields of SELECT statement.

So, if you have common name field in your three tables, you must explicit.

If you use a notation like this SELECT a.* ... you'll show all fields of your aliased table a but the name of field in the header column is the same of the table structure.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.