简体   繁体   English

如何使用两个子查询从两个表中提取值? T-SQL

[英]How to use two sub queries to pull values from two tables? T-SQL

First off I want to apologize if I'm asking the completely wrong question - I'm a beginner when it comes to SQL and I'm not sure how to accomplish my goal, but I am assuming based off my research that sub queries are what I need to be working with. 首先,如果我要问一个完全错误的问题,我想道歉-我是SQL的初学者,我不确定如何实现我的目标,但是我基于我的研究假设子查询是我需要与之合作的东西。

I have two tables, one with time card data (Table 1), and another with high level project data(Table 2). 我有两个表,一个表包含考勤卡数据(表1),另一个表具有高级项目数据(表2)。

Table 1 : 表1

+------+------------------+---------------+-------------+
| code | work_description | resource_name | total_hours |
+------+------------------+---------------+-------------+
|  101 | Time Reporting   | Jane Doe      |           5 |
|  101 | Time Reporting   | Jane Doe      |           7 |
|  101 | Time Reporting   | Jane Doe      |           9 |
|  201 | Time Reporting   | Joe Smith     |           2 |
|  201 | Time Reporting   | Joe Smith     |           4 |
|  201 | Time Reporting   | Joe Smith     |           6 |
+------+------------------+---------------+-------------+

Table 2 : 表2

+------+------------+----------------+
| code | project_id |     descr      |
+------+------------+----------------+
|  100 |        100 | Project A      |
|  101 |        100 | Time Reporting |
|  102 |        100 | Milestones     |
|  103 |        100 | Planning       |
|  200 |        200 | Project B      |
|  201 |        200 | Time Reporting |
|  202 |        200 | Milestones     |
|  203 |        200 | Planning       |
+------+------------+----------------+

In table 2, when the code column is equal to the project_id column, descr shows the project name. 在表2中,当code列等于project_id列时,descr显示项目名称。 I need to pull all of table 1, in addition to the project name that corresponds to each row. 除了对应于每一行的项目名称之外,我还需要提取所有表1。

What I need: 我需要的:

+-----------+------+------------------+---------------+-------------+
|  descr    | code | work_description | resource_name | total_hours |
+-----------+------+------------------+---------------+-------------+
| Project A |  101 | Time Reporting   | Jane Doe      |           5 |
| Project A |  101 | Time Reporting   | Jane Doe      |           7 |
| Project A |  101 | Time Reporting   | Jane Doe      |           9 |
| Project B |  201 | Time Reporting   | Joe Smith     |           2 |
| Project B |  201 | Time Reporting   | Joe Smith     |           4 |
| Project B |  201 | Time Reporting   | Joe Smith     |           6 |
+-----------+------+------------------+---------------+-------------+

My though process is that first I would have to find the project_id that relates to each row in table 1. Then, I could use that value to match against project_id in table 2, so I could pull the project name out of the descr column 我的流程虽然是,首先我必须找到与表1中每一行相关的project_id。然后,我可以使用该值与表2中的project_id进行匹配,因此可以将项目名称从descr列中拉出。

Here is what I have so far. 这是我到目前为止所拥有的。 This correctly pulls the project ID (I don't know if this is best practice). 这样可以正确提取项目ID(我不知道这是否是最佳实践)。 I have tried a few different sub queries for the project name, but I haven't been able to do it right yet. 我已经尝试了几个不同的子查询来查询项目名称,但是现在还无法执行。

SELECT  
    (SELECT t2.code WHERE t1.code=t2.code) as found_project_id,
    t2.descr,
    t1.code,
    t1.work_description,
    t1.resource_name,
    t1.total_hours
FROM Table1 as t1   
    INNER JOIN Table2 as t2 ON t1.code=t2.code

So my question is, how can I use sub queries (or any other method) to pull all of table 1 in addition to the project names? 所以我的问题是,除了项目名称之外,我如何才能使用子查询(或任何其他方法)提取所有表1?

You don't need a subquery here. 您在这里不需要子查询。 A simple join is enough since an inner join already accomplishes what you are trying to do with the subquery: 一个简单的联接就足够了,因为内部联接已经完成了您要对子查询执行的操作:

SELECT
    proj.descr,
    t2.code,
    t1.work_description,
    t1.resource_name,
    t1.total_hours
FROM table2 t2

JOIN table1 t1 ON
    t1.code = t2.code
JOIN table2 proj ON
    proj.code = t2.project_id

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

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