I have a database with tables for "users" and "profiles". Every profile links back to a user. I would like to do an inner join where I am able to retrieve a set of profiles that meet a condition along with the user data associated with them. However some columns in both profiles and users have the same names.
Currently I have a few methods I know I could do:
- I know I can do a select * on profiles and then programmatically for each profile also select * on users for the user of the profile.
- I could also do an alias for the columns with duplicate names but this would require maintenance if I were to update the columns in the future.
- I could also require every column to have a unique name, but that feels like not a good solution either.
Is there a better way? What is the best way to do this long term?
Per request, lets say the tables are defined as the following:
- Table users having columns of id, name
- Table profiles having columns of id, user_id, name
SELECT t1.col1 AS t1c1, t2.col1 AS t2c1, ... FROM table AS t1 JOIN table AS t2 ...
. PS. Do not use an asterisk as "all columns" alias except COUNT(*), always type columns names one-by-one.