如何在我的第一次选择中使用第二次选择的结果 [英] How to use the result from a second select in my first select

查看:28
本文介绍了如何在我的第一次选择中使用第二次选择的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用第二个 SELECT 来获取一些 ID,然后在第二个 SELECT 中使用该 ID,但我不知道如何.

I am trying to use a second SELECT to get some ID, then use that ID in a second SELECT and I have no idea how.

SELECT Employee.Name 
FROM Emplyee, Employment 
WHERE x = Employment.DistributionID 
(SELECT Distribution.DistributionID FROM Distribution 
       WHERE Distribution.Location = 'California') AS x

这篇文章很长,但这里有一个简短的提示"

虽然我选择的语法不好,但逻辑不是.我需要那个x".因此第二个 select 是最重要的.然后我必须在第一个 select 中使用x".我只是不知道如何

While the syntax of my select is bad, the logic is not. I need that "x" somehow. Thus the second select is the most important. Then I have to use that "x" within the first select. I just don't know how

/提示

这是我唯一能想到的,我对Sql很新,我想在练习之前我需要一本书,但现在我已经开始了我想完成我的小程序.

This is the only thing I could imagine, I'm very new at Sql, I think I need a book before practicing, but now that I've started I'd like to finish my small program.

好吧,我查了联接,还是没明白

Ok I looked up joins, still don't get it

SELECT Employee.Name 
    FROM Emplyee, Employment 
    WHERE x = Employment.DistributionID 
    LEFT JOIN Distribution ON
    (SELECT Distribution.DistributionID FROM Distribution 
           WHERE Distribution.Location = 'California') AS x

ASLeft

我使用名称从上面的红色中找到 ID,我使用在下面的表格中从上面的红色中找到的 ID.然后我将找到的 ID 与 Green 匹配.我用Green ID找对应的Name

I use name to find ID from upper red, I use the ID I find FROM upper red in lower table. Then I match the ID I find with Green. I use Green ID to find corresponding Name

我有 California 作为 C# 的输出数据.我想使用 California 来查找 DistributionID.我使用 DistributionID 来查找 EmployeeID.我使用 EmployeeID 来查找姓名

I have California as output data from C#. I want to use California to find the DistributionID. I use the DistributionID to find the EmployeeID. I use EmployeeID to find Name

我的逻辑:

Parameter: Distribution.Name (from C#)

Find DistributionID that has Distribution.Name
Look in Employment WHERE given DistributionID 
      reveals Employees that I am looking for (BY ID)
Use that ID to find Name
      return Name

表格:

注意:在这个示例图片中,员工因为选择而重复,他们实际上是单数

NOTE: In this example picture the Employee repeats because of the select, they are in fact singular

在Locatie"(中间表)是位置,我(再次)从 C# 获取位置,我以 California 为例.我需要首先找到ID

In "Locatie" (middle table) is Location, I get location (again) from C#, I use California as an example. I need to find the ID first and foremost!

抱歉,它们不是英文,但这里是创建表:

Sory they are not in english, but here are the create tables:

推荐答案

试试这个:

SELECT angajati.Nume 
FROM angajati 
JOIN angajari  ON angajati.AngajatID = angajari.AngajatID
JOIN distribuire ON angajari.distribuireid = distribuire.distribuireid
WHERE distribuire.locatie = 'california'

因为您有一张表将员工映射到他们的分布位置,所以您只需要在中间加入该表即可创建映射.如果您喜欢 WHERE 子句,您可以使用变量,这样您就可以将其作为存储过程或从 C# 代码输出中需要的任何内容调用.

As you have a table mapping employees to their distribution locations, you just need to join that one in the middle to create the mapping. You can use variables if you like for the WHERE clause so that you can call this as a stored procedure or whatever you need from the output of your C# code.

这篇关于如何在我的第一次选择中使用第二次选择的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆