简体   繁体   中英

PostgreSQL join query issue

I am learning PostgreSQL and was practicing questions.

I am stuck at this question here

My Solution does not seem to work and I am not sure why:

SELECT first_name, last_name, department_id, department_name 
FROM employees 
JOIN departments ON departments.department_id = employees.department_id;

Their solution:

SELECT first_name, last_name, department_id, department_name 
FROM employees 
JOIN departments USING (department_id);

I'd really appreciate the help.

Your solution does not work because the field names are ambiguous. Try prefixing like this

SELECT 
  employees.first_name, 
  employees.last_name, 
  departments.department_id, 
  departments.department_name 
FROM employees 
JOIN departments ON departments.department_id = employees.department_id;

The problem does not appear when you use USING , because then there will be only a single department_id column in the join result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM