简体   繁体   English

在SQL Server中联接两个表

[英]Joining two tables in SQL Server

I have two tables. 我有两张桌子。 One called Employee and the other called Departments 一个叫雇员,另一个叫部门

These are the two tables: 这是两个表:

CREATE TABLE Departmenr (
    department_code NCHAR(4),
    department_name NVARCHAR(15),
    city NVARCHAR(20),
    budget MONEY
)

CREATE TABLE Employee (
    employee_id NCHAR(6), 
    name NVARCHAR(20), 
    position NVARCHAR(20),
    salary MONEY, 
    dcode NCHAR(3),
)

I have to write a statement that lists the name of each employee and name of the department they work in, for all employees with who have a salary over £20,000. 我必须写一份声明,列出所有年薪超过20,000英镑的员工的姓名和所从事部门的名称。 This means I have to join the Employee and Department tables to get an output. 这意味着我必须联接Employee和Department表以获得输出。

I thought it might be something like this: 我认为可能是这样的:

SELECT Emplyee.name, Department.department_name
FROM Employee
FULL OUTER JOIN Department 
ON Employee.salary > 20000;

but it has errors. 但有错误。 How do I do this? 我该怎么做呢?

Assuming dcode is a foreign key for table department you can do: 假设dcode是表部门的外键,则可以执行以下操作:

SELECT e.NAME,d.department_name
FROM Employee e
INNER JOIN Department d ON e.dcode = d.department_code
WHERE e.salary > 20000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM