简体   繁体   English

PostgreSQL 连接查询问题

[英]PostgreSQL join query issue

I am learning PostgreSQL and was practicing questions.我正在学习 PostgreSQL 并且正在练习问题。

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.使用USING时不会出现问题,因为那时连接结果中将只有一个department_id列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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