4 +表之间的连接或条件逻辑

时间:2017-11-17 00:26:42

标签: sql oracle join

完全披露,这是一个功课问题,但我还有20多个这些问题要困难得多,我真的无法绕过一些比较复杂的陈述,所以我会这样做。在我完全放弃作业之前,请欣赏帮助。教授和TA拒绝了任何帮助。

我正在使用' HR'在所有Oracle DB中作为演示提供的表。 我需要创建一个特定部门中所有工作的唯一列表以及位置。它有以下表格的表格:地区,地点,部门,工作和员工:

位置:

 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 LOCATION_ID                   NOT NULL NUMBER(4)
 STREET_ADDRESS                     VARCHAR2(40)
 POSTAL_CODE                        VARCHAR2(12)
 CITY                      NOT NULL VARCHAR2(30)
 STATE_PROVINCE                     VARCHAR2(25)
 COUNTRY_ID                     CHAR(2)

工作:

 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 JOB_ID                    NOT NULL VARCHAR2(10)
 JOB_TITLE                 NOT NULL VARCHAR2(35)
 MIN_SALARY                     NUMBER(6)
 MAX_SALARY                     NUMBER(6)

雇员:

 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMPLOYEE_ID                   NOT NULL NUMBER(6)
 FIRST_NAME                     VARCHAR2(20)
 LAST_NAME                 NOT NULL VARCHAR2(25)
 EMAIL                     NOT NULL VARCHAR2(25)
 PHONE_NUMBER                       VARCHAR2(20)
 HIRE_DATE                 NOT NULL DATE
 JOB_ID                    NOT NULL VARCHAR2(10)
 SALARY                         NUMBER(8,2)
 COMMISSION_PCT                     NUMBER(2,2)
 MANAGER_ID                     NUMBER(6)
 DEPARTMENT_ID                      NUMBER(4)

部门:

 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 DEPARTMENT_ID                 NOT NULL NUMBER(4)
 DEPARTMENT_NAME               NOT NULL VARCHAR2(30)
 MANAGER_ID                     NUMBER(6)
 LOCATION_ID                        NUMBER(4)

当然,我可以考虑关系代数的类型,但我不知道它将如何用于SQL语法。我当然知道如何做更多琐碎的联接,但我不理解如何将所有要求集中到一个语句中。我想得到DEPARTMENT_ID = 80的所有UNIQUE或DISTINCT JOB_ID,然后对Jobs进行连接以获得匹配的JOB_TITLE,然后从DEPARTMENTS.DEPARTMENT_ID派生LOCATION_ID,最后将Locations连接到我原来的JOB_TITLE选项WHERE LOCATION.LOCATION_ID = DEPARTMENTS.DEPARTMENT_ID 。那看起来是什么样的。我必须做3个连接才能在一个选择中获取所有信息吗?

3 个答案:

答案 0 :(得分:0)

像这样的Smth,也许是

新时代的加入:

select tloc.*, tdept.*, tjobs.* 
from locations tloc
  inner join departments tdept on (tloc.location_id=tdept.location_id)
  inner join employees templ on (tdept.department_id=templ.department_id)
  inner join jobs tjobs on (templ.employee_id=tjobs.employee_id)
where 
  tdept.department_id=80

老式的加入:

select tloc.*, tdept.*, tjobs.* 
from locations tloc, departments tdept, employees templ, jobs tjobs 
where 
  tloc.location_id=tdept.location_id 
  and tdept.department_id=templ.department_id 
  and templ.employee_id=tjobs.employee_id
  ----
  and tdept.department_id=80

答案 1 :(得分:0)

这可能会对你有所帮助。添加' Where'条件根据您的要求。

@echo off
set Noms=(bni csc cba lpz oro pnd pts scz trj)
set Dirs=(8.8.8.8 8.8.8.4 8.8.8.4 8.8.8.8 8.8.8.8 8.8.8.4 8.8.8.8 8.8.8.4 8.8.8.8)
rem --------------------------------------------------------------
rem Builds .txts with names in list Noms pinging Addresses in Dirs
rem --------------------------------------------------------------
for %%j in %Noms% do (
set direccion = %Dirs[%%j]%
ping /n 3 %direccion% > c:\log\p0.txt
findstr "Media" c:\log\p0.txt > c:\log\p1.txt
for /f "tokens=3 delims=," %%i in (c:\log\p1.txt) do @echo %%i > c:\log\p2.txt
for /f "tokens=4 delims=<equal> " %%i in (c:\log\p2.txt) do @echo %%i > c:\log\p0.txt
for /f "tokens=1 delims=m" %%i in (c:\log\p0.txt) do (set pingvar=%%i)
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b) 
For /f "tokens=1-3 delims=/:," %%a in ("%TIME%") do (set mytime=%%a:%%b:%%c) 
set totvar=%mydate%     %mytime%    %pingvar%
echo %totvar% > c:\log\%%j.txt
)

答案 2 :(得分:0)

这是为了让您入门。您需要的核心来自2个表employee(用于job_id)和department(用于location_id),员工和部门之间的关系是通过共享的department_id列(这成为连接)

SELECT
      e.JOB_ID
    , d.LOCATION_ID
FROM Employees e
INNER JOIN Departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID
WHERE e.DEPARTMENT_ID = 80

现在你需要弄清楚如何获得&#34;唯一性&#34;

一种方法是添加select distinct,虽然这会有效,但确实会限制您的选项

SELECT DISTINCT
      e.JOB_ID
    , d.LOCATION_ID
FROM Employees e
INNER JOIN Departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID
WHERE e.DEPARTMENT_ID = 80

或者,使用GROUP BY打开其他可能性,例如聚合

SELECT
      e.JOB_ID
    , d.LOCATION_ID
    , COUNT(*) as count_of_employees
FROM Employees e
INNER JOIN Departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID
WHERE e.DEPARTMENT_ID = 80
GROUP BY
      e.JOB_ID
    , d.LOCATION_ID

我留给您的是如何将名称带入结果中。