I have to deal with Java to create a restful API and I'm using it with MySQL to query the DB.
I'm quite new to Java and this might be a very basic question, but used to PHP frameworks where this problem doesn't exist and I was wondering what's the way to do it in Java.
I'm querying 3 tables joining them by their foreign keys and I found out that if two of those tables has the same column name, only one of them will be returned. The one from the latest table declared in the from
statement. It seems they get override.
For example, if the tables type_application
and type_leaves
have both a name
column, only the name
column from type_leaves
will be returned in this query:
select * from leaves, type_applications, type_leaves
where leaves.type_leave = type_leaves.id and
leaves.type_application = type_applications.id;
Now, I know this can easily be solved by specifying all the needed column names prefixed with the table name (in case they are duplicated) and using as
to create an alias name:
select leaves.id, type_leaves.name as leaves_name, type_applications.name as application_name
from leaves, type_applications, type_leaves
where leaves.type_leave = type_leaves.id and
leaves.type_application = type_applications.id;
But this doesn't sound like the best solution for me. I would rather keep using the *
in order to get all fields (which is what I will always want).
This will also help to have an smaller query, easier to read and easier to maintain in case I keep adding or deleting columns in the table with the time.
Any solution for this? What's the way to deal with it in Java?