3

我试图用一秒钟SELECT来获取一些 ID,然后在一秒钟内使用该 ID,但SELECT我不知道如何。

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

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

虽然我选择的语法不好,但逻辑却不是。我需要那个“x”。所以第二个select是最重要的。然后我必须在第一个select. 我只是不知道如何

/小费

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

编辑:

好的,我查了连接,还是不明白

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

AS在和获取错误消息Left

我使用名称从上红色中查找 ID,我使用从下表中的上红色中找到的 ID。然后我将找到的 ID 与 Green 匹配。我使用Green ID来查找对应的Name

在此处输入图像描述

我有California来自 C# 的输出数据。我想用它California来查找 DistributionID。我使用 DistributionID 来查找 EmployeeID。我使用 EmployeeID 来查找 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

表:

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

在“Locatie”(中间表)中是位置,我(再次)从 C# 中获取位置,我California以此为例。ID我首先要找到!

在此处输入图像描述

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

在此处输入图像描述

4

3 回答 3

2

试试这个解决方案:

DECLARE @pLocatie VARCHAR(40)='Alba'; -- p=parameter

SELECT a.AngajatID, a.Nume
FROM Angajati a
JOIN Angajari j ON a.AngajatID=j.AngajatID
JOIN Distribuire d ON j.DistribuireID=d.DistribuireID
WHERE d.Locatie=@pLocatie

您应该在 Angajari 表 (Employment) 上添加一个唯一键,因此:

ALTER TABLE Angajari
ADD CONSTRAINT IUN_Angajari_AngajatID_DistribuireID UNIQUE (AngajatUD, DistribuireID);

这将防止重复(AngajatID, DistribuireID).

于 2013-02-07T23:18:44.933 回答
2

尝试这个:

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# 代码的输出中调用任何您需要的内容。

于 2013-02-08T14:21:11.597 回答
0

I don't know how you are connecting Emplyee(sic?) and Employment, but you want to use a join to connect two tables and in the join specify how the tables are related. Joins usually look best when they have aliases so you don't have to repeat the entire table name. The following query will get you all the information from both Employment and Distribution tables where the distribution location is equal to california. You can join employee to employment to get name as well.

SELECT *
FROM Employment e
JOIN Distribution d on d.DistributionID = e.DistributionID
WHERE d.Location = 'California'

This will return the contents of both tables. To select particular records use the alias.[Col_Name] separated by a comma in the select statement, like d.DistributionID to return the DistributionID from the Distribution Table

于 2013-02-07T21:26:03.493 回答