简体   繁体   English

连接具有特定列的两个表

[英]Joining two tables with specific columns

I am new to SQL, I know this is really basic but I really do not know how to do it!我是 SQL 新手,我知道这真的很基础,但我真的不知道该怎么做! I am joining two tables, each tables lets say has 5 columns, joining them will give me 10 columns in total which I really do not want.我正在加入两个表,每个表可以说有 5 列,加入它们总共会给我 10 列,我真的不想要。 What I want is to select specific columns from both of the tables so that they only show after the join.我想要的是从两个表中选择特定的列,以便它们仅在连接后显示。 (I want to reduce my joining result to specific columns only) (我只想将我的加入结果减少到特定的列)

SELECT * FROM tbEmployees

JOIN tbSupervisor

ON tbEmployees.ID = tbSupervisor.SupervisorID

The syntax above will give me all columns which I don't want.上面的语法会给我所有我不想要的列。 I just want EmpName, Address from the tblEmployees table and Name, Address, project from the tbSupervisor table我只想要 tblEmployees 表中的 EmpName, Address 和 tbSupervisor 表中的 Name, Address, project

I know this step:我知道这一步:

SELECT EmpName, Address FROM tbEmployees

JOIN tbSupervisor

ON tbEmployees.ID = tbSupervisor.SupervisorID

but I am not sure about the supervisor table.但我不确定主管表。

I am using SQL Server.我正在使用 SQL Server。

This is what you need:这是你需要的:

Select e.EmpName, e.Address, s.Name, S.Address, s.Project
From tbEmployees e
JOIN tbSupervisor s on e.id = SupervisorID

You can read about this on W3Schools for more info.您可以在W3Schools上阅读有关此内容的更多信息。

You can get columns from specific tables, either by their full name or using an alias:您可以通过全名或使用别名从特定表中获取列:

SELECT E.EmpName, E.Address, S.Name, S.Address, S.Project
FROM tbEmployees E
INNER JOIN tbSupervisor S ON E.ID = S.SupervisorID

You can use the table name as part of the column specification:您可以使用表名作为列规范的一部分:

SELECT tbEmployees.EmpName, tbEmployeesAddress, tbSupervisor.Name,
       tbSupervisor.Address, tbSupervisor.project

FROM tbEmployees

JOIN tbSupervisor

ON tbEmployees.ID = tbSupervisor.SupervisorID


    SELECT employees.EmpName, employees.Address AS employeer address, 
           supervisor.Name, supervisor.Address AS supervisor address,supervisor.project 
    FROM tbEmployees 
       AS employees 
    JOIN tbSupervisor 
       AS supervisor 
    ON 
       employees.ID = supervisor.SupervisorID


You need to learn about aliases.您需要了解别名。 They will make your queries more maintainable.它们将使您的查询更易于维护。 Also, you should always use aliases when referencing columns, so your query is clear about what it is doing:此外,在引用列时您应该始终使用别名,以便您的查询清楚地了解它在做什么:

SELECT e.EmpName, e.Address, s.name, s.address as SupervisorAddress
FROM tbEmployees e  JOIN
     tbSupervisor s
     ON e.ID = s.SupervisorID;

Note that I also renamed the second address so its name is unique.请注意,我还重命名了第二个地址,因此其名称是唯一的。

Specify the table name and field name in your selection在您的选择中指定表名和字段名

SELECT tbEmployees.EmpName,
       tbEmployees.Address,
       tbSupervisor.[column name]
  FROM tbEmployees
  JOIN tbSupervisor ON tbEmployees.ID = tbSupervisor.SupervisorID
SELECT product_report.*, 
       product.pgroup 
FROM   `product_report` 
       INNER JOIN product 
               ON product_report.product_id = product.id 
WHERE  product.pgroup = '5' 
ORDER  BY product.id DESC 

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

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