使用联接进行高级查询

时间:2018-11-16 20:43:09

标签: sql sql-server tsql

我创建了以下表格:

def find_outlier(integers):
    lst_odd = [] # makin new odd list
    lst_even = [] # making new even list
    for i in integers:
     # checking if these numbers are even or odd.
        if i % 2 == 0 :
             # if even they will be added in this list otherwise only one is added.
            lst_even.append(i) 
        else:
     # if odd, they are added here, otherwise, only one is added
            lst_odd.append(i)

    #checking if whether these two list contains only one element.  if so we return that element.
    if len(lst_odd) == 1:
        return lst_odd[0]
    else:
        return lst_even[0]

我已根据表格要求插入了数据。

我正在研究以下问题:让所有员工的名字和姓氏,以及部门代码和薪水

我已经为这个问题写了这个查询,但是没有用。

CREATE TABLE Employees 
(
    EmployeeID      int         IDENTITY(1,1) PRIMARY KEY
    ,EmployeeNumber int         UNIQUE
    ,DateOfBirth    datetime        NOT NULL
    ,FirstName      nvarchar(14)    NOT NULL
    ,MiddleName     nvarchar(14)    NOT NULL
    ,LastName       nvarchar(16)    NOT NULL
    ,DateHired      datetime        NOT NULL 
)

CREATE TABLE Customers 
(
    CustomerID      int         IDENTITY(1,1) PRIMARY KEY
    ,FirstName      nvarchar(14)    NOT NULL
    ,MiddleName     nvarchar(14)    NOT NULL
    ,LastName       nvarchar(16)    NOT NULL
    ,DateLastVisited datetime       NOT NULL 
    ,EmailAddress   nvarchar(52)    NOT NULL
)

CREATE TABLE Departments 
(
    DepartmentID    int         IDENTITY(1,1) PRIMARY KEY
    ,Code           nchar(4)        UNIQUE
    ,Name           nvarchar(40)    NOT NULL 
)

CREATE TABLE DepartmentEmployees 
(
    DepartmentEmployeeID    int     IDENTITY(1,1) PRIMARY KEY
    ,EmployeeID     int             NOT NULL
            CONSTRAINT Department_Employee REFERENCES Employees(EmployeeID)
    ,DepartmentID   int         NOT NULL
            CONSTRAINT Employee_Department REFERENCES Departments(DepartmentID)
    ,DateStarted    datetime        NOT NULL
    ,DateEnded      datetime        NOT NULL 
)

CREATE TABLE Salaries 
(
    SalaryID        int         IDENTITY(1,1) PRIMARY KEY
    ,EmployeeID     int         NOT NULL
            CONSTRAINT Salaried_Employee REFERENCES Employees(EmployeeID)
    ,Amount     money           NOT NULL
    ,DateStarts     datetime        NOT NULL
    ,DateEnds       datetime        NOT NULL 
)

请帮助我

2 个答案:

答案 0 :(得分:1)

SELECT  E.FirstName,E.LastName, D.Code,S.Amount From Employees E
Inner Join Salaries S
On E.EmployeeID= S.EmployeeID
Inner join Departments D
**on D.DepartmentID= E.EmployeeID**

确定要将DepartmentID和EmployeeID一起加入吗?

答案 1 :(得分:0)

此查询将为您提供所需的一切

select  firstname, lastname ,Departments.Code as DepartmentCode,Salaries.Amount  as Salary
from 
Employees 
inner join DepartmentEmployees on Employees.EmployeeID = DepartmentEmployees.EmployeeID  
inner join Departments on   DepartmentEmployees.DepartmentID   = Departments.DepartmentID  
inner join Salaries on Employees.EmployeeID = Salaries.EmployeeID 

每个员工都属于一个部门(DepartmentEmployees)。因此,您必须与DepartmentEmployees一起加入Employee。在下一个联接中,您可以找到部门代码,在第三个联接中,您可以从“薪金”表中找到薪水金额。