1

我有一张这样的桌子:

电子邮件_tbl

plcid       Ecode
----------- -----------
23          001646
24          001646
25          E004
25          2274
25          2208
25          1868

我还有一张桌子Employee

Ecode   Ename
E004     jaseem
2274     jasir
2208      deepu
1868       rupa
001646     shafeer

我想得到所以我Enameplcid=25这里使用这个查询:

SELECT em.Ename
FROM dbo.Email_tbl e
JOIN dbo.EmployeeMaster_tbl em ON em.Ecode IN (SELECT Ecode
                                               FROM Email_tbl
                                               WHERE plcid = 25)

但我的结果是错误的。我的查询有什么问题?

4

3 回答 3

7
SELECT  em.Ename
FROM    dbo.Email_tbl e
        JOIN dbo.EmployeeMaster_tbl em ON em.Ecode = e.ECode
WHERE e.plcid = 25 
于 2013-09-10T12:27:42.360 回答
2

你为什么还要加入桌子?为什么有 3 个答案“修复”不必要的连接?

SELECT em.Ename
FROM dbo.EmployeeMaster_tbl em
WHERE em.Ecode IN (SELECT Ecode
                   FROM Email_tbl
                   WHERE plcid = 25)
于 2013-09-10T12:30:48.710 回答
0
    SELECT emp.Ename
    FROM Email_tbl e INNER JOIN Employee emp ON (e.Ecode = emp.Ecode)
    WHERE e.plcid = 25

这将导致多行,因为 Employee 中的多行具有 PLCID = 25 的 Ecode

于 2013-09-10T12:28:04.460 回答