I have two tables in PostgreSQL as follows:
Table "users", with the following fields:
- user_id : incremental integer, and primary key of the table
- user_name : varchar
and another "data" table with the following fields:
- data_id: incremental integer, and primary key
- description: varchar
- created_by: integer and foreign key of user_id of the users table
- modified_by: integer and foreign key of user_id of the users table
The query I want to obtain is the data_id, description, and user_name of the users referenced in created_by and modified_by
What I tried was the following:
SELECT data_id, descripcion, users.user_name FROM data
LEFT JOIN users ON users.user_id = data.created_by
LEFT JOIN users ON users.user_id = data.modified_by
and it gave me the error I expected:
table name "users" specified more than once
In addition to that select
I am calling user_name
only once, and not twice as it should be, but I have no idea what else to do.
Try as follows:
users
for each time you invoke it in theLEFT JOIN
SELECT
it you will choose more than once from the same table the desired values, but instead of doing it to the tableusers
you would do it to one of the 2 aliases that you placed in said tableCode
you have to use aliases.