我尝试过子查询和连接查询,但我无法得到正确的答案,因为我需要从不同的表中检索数据。这就是我想要做的事情:
显示部门名称,街道地址和 城市的部门位置和国家名称 部门所在地的员工 名为Den Raphaely的名字
我有这样的桌子。
COUNTRIES TABLE
DEPARTMENT TABLE
员工表
JOBS_HISTORY TABLE
JOB TABLE
LOCATIONS TABLE
REGION TABLE
这是尝试过的查询
SELECT Employees.FIRST_NAME, Employees.LAST_NAME,
Departments.DEPARTMENT_NAME, Locations.Street_Address,
Locations.City, Countries.Country_Name FROM
Countries INNER JOIN Locations ON
Countries.Country_ID=Locations.Country_ID
INNER JOIN Departments ON
Locations.Location_id=Departments.LOCATION_ID
INNER JOIN Employees ON
Departments.DEPARTMENT_ID = (SELECT Employees.DEPARTMENT_ID FROM Employees
WHERE FIRST_NAME LIKE 'Den' AND LAST_NAME LIKE 'Raphaely')
答案 0 :(得分:1)
您的JOIN与过滤(WHERE)逻辑混淆:
SELECT
Employees.FIRST_NAME,
Employees.LAST_NAME,
Departments.DEPARTMENT_NAME,
Locations.Street_Address,
Locations.City,
Countries.Country_Name
FROM Countries
INNER JOIN Locations ON Countries.Country_ID=Locations.Country_ID
INNER JOIN Departments ON Locations.Location_id=Departments.LOCATION_ID
INNER JOIN Employees ON Departments.DEPARTMENT_ID = Employees.DEPARTMENT_ID
WHERE Employees.FIRST_NAME LIKE 'Den' AND Employees.LAST_NAME LIKE 'Raphaely'
答案 1 :(得分:0)
SELECT e.first_name, e.last_name, d.department_name, l.street_address, l.city, c.country_name
FROM
employees e
INNER JOIN
department d
ON e.department_id = d.department_id
INNER JOIN
location l
ON d.location_id = l.location_id
INNER JOIN country c
ON c.country_id = l.country_id
WHERE
e.first_name = 'Den'
AND
e.last_name = 'Raphaely'