【问题标题】:SQL Query Display the names of the employees and their corresponding departmentSQL Query 显示员工姓名及其对应部门
【发布时间】:2015-07-16 09:35:36
【问题描述】:

以下是我的CREATE TABLE 脚本:

CREATE TABLE Person 
( 
   ID INT Primary Key Identity (1,1), 
   LastName nVarchar (20) not NULL, 
   FirstName nVarchar (20) not NULL, 
   MiddleName nVarchar (20), 
   BirthDate DateTime not NULL, 
   Age INT not NULL, 
   Check (Age18) 
); 

CREATE TABLE Department 
( 
   ID INT Primary Key Identity (1,1), 
   DepartmentName nVarchar (50) Unique, 
   DepartmentCode nVarchar (20) Unique, 
   IsActive Bit Default (1) 
); 

CREATE TABLE Employee 
( 
   ID INT Primary Key Identity (1,1), 
   PersonId INT Foreign Key REFERENCES Person, 
   DepartmentId INT Foreign Key REFERENCES Department, 
   Salary Decimal (18,2), 
   Check (Salary10000), 
   IsActive Bit Default (1) 
); 

以下是我的INSERT 脚本:

INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Dela Cruz','Juan',NULL,01/01/199,22) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Dela Cerna','Pedro','Juan',11/01/1993,21) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Villaflores','Rachel','Diacoma',10/7/1990,24) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Abendan','Marnell',NULL,03/15/1989,25) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Oplado','Aiza','Tapayan',11/18/1993,21) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Loreto','Desire','Talingting',06/10/1993,21) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Magbanua','Prince Laurence','Rallos',05/25/1992,22) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Locsin','Franz Cyril',NULL,02/14/1993,21) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Dela Pena','Precious',NULL,01/01/199,21) 

 INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('ITDepartment','ItDept',1) 
INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('EngineeringDepartment','EDept',1) 
INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('ComputerScienceDepartment','CSDept',1) 
INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('InformationSystemDepartment','ISDept',1) 
INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('BusinessAdministrationDepartment','BADept',1) 
INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('ElementaryDepartment','ElemDept',1) 

 INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (1,2,12000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (2,4,10001,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (3,6,13000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (4,1,25000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (5,3,15000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (6,5,10002,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (7,1,56000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (8,4,14000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (9,6,15900,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (2,4,12300,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (4,1,13500,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (6,3,14300,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (8,5,12500,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (1,2,11460,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (3,4,10910,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (5,6,10001,1) 

以下是我的选择脚本:

SELECT 
    Person.LastName, Department.DepartmentName 
FROM 
    Person, Department 
FULL OUTER JOIN 
    Employee ON Employee.PersonId = Employee.DepartmentId 

这是不正确的结果。我不知道什么是正确的。这是我第一次做JOINS。

Dela Cruz   BusinessAdministrationDepartment 
Dela Cerna  BusinessAdministrationDepartment 
Villaflores BusinessAdministrationDepartment 
Abendan BusinessAdministrationDepartment 
Oplado  BusinessAdministrationDepartment 
Loreto  BusinessAdministrationDepartment 
Magbanua    BusinessAdministrationDepartment 
Locsin  BusinessAdministrationDepartment 
Dela Pena   BusinessAdministrationDepartment 
Dela Cruz   ComputerScienceDepartment 
Dela Cerna  ComputerScienceDepartment 
Villaflores ComputerScienceDepartment 
Abendan ComputerScienceDepartment 
Oplado  ComputerScienceDepartment 
Loreto  ComputerScienceDepartment 
Magbanua    ComputerScienceDepartment 
Locsin  ComputerScienceDepartment 
Dela Pena   ComputerScienceDepartment 

我在计算机科学系停了下来,因为它太长了。我怎样才能做到这一点?请帮忙。谢谢! :)

【问题讨论】:

  • 检查一些最近在线网站的语法。这里的问题是旧的(如“在渡渡鸟的路上”)连接语法与新的连接语法的混合以及基础知识的缺失:这是一个太长的问题,无法在这里解决。
  • Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已替换为 ANSI 中的 proper ANSI JOIN 语法-92 SQL 标准(20 多年前),不鼓励使用它
  • 感谢您的启发。我想用。假设我不会使用逗号分隔的表格列表。您将如何使用正确的 ANSI JOIN 语法来做到这一点?谢谢:)

标签: sql sql-server sql-server-2008 sql-server-2014 sql-server-2014-express


【解决方案1】:

你在找这个吗:-

Select  p.LastName, d.DepartmentName
From    Employee As e
        Join Person As p On e.PersonId = p.Id
        JOin Department As d On e.DepartmentId = d.ID

【讨论】:

  • 非常感谢,这完全帮助了我!我现在正在研究这个!
【解决方案2】:

SELECT Person.LastName, Department.DepartmentName FROM Employee INNER JOIN Person ON Employee.PersonId = Person.ID INNER JOIN Department ON Employee.DepartmentId = Department.ID

【讨论】:

  • 非常感谢,这完全帮助了我!我现在正在研究这个!
猜你喜欢
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
  • 2021-07-22
  • 2022-12-19
  • 1970-01-01
相关资源
最近更新 更多