我正在尝试通过此输入来获取first_name Georgi和last_name Facello的所有雇员的头衔值:
SELECT employees.emp_no, title
from employees,
employees.titles
where first_name = 'Georgi'
and last_name = 'Facello';
员工数据库:
# emp_no, birth_date, first_name, last_name, gender, hire_date
'10001', '1953-09-02', 'Georgi', 'Facello', 'M', '1986-06-26'
'10002', '1964-06-02', 'Bezalel', 'Simmel', 'F', '1985-11-21'
Employees.titles:
# emp_no, title, from_date, to_date
'10001', 'Senior Engineer', '1986-06-26', '9999-01-01'
'10002', 'Staff', '1996-08-03', '9999-01-01'
但是此输入提供以下输出:
# emp_no, title
'10001', 'Senior Engineer'
'55649', 'Senior Engineer'
'10001', 'Staff'
'55649', 'Staff'
'10001', 'Senior Engineer'
'55649', 'Senior Engineer'
此输出达到999+,这显然是不正确的,有什么想法如何获得正确的输出吗?
答案 0 :(得分:0)
您需要JOIN
而不是CROSS JOIN
:
SELECT es.emp_no, t.title
FROM employees es INNER JOIN
employees.titles t
ON t.emp_no = es.emp_no
WHERE es.first_name = 'Georgi' AND es.last_name = 'Facello';
使用始终正确的显式JOIN
语法,而不要在FROM
子句中使用逗号