我有100人的桌子EMPLOYEE。我想写一个查询来查找具有相同BIRTHDATE的员工对。
结果应该为对中的每个雇员返回EMPNO,LASTNAME和BIRTHDATE(6列结果表)。
我认为这样的事情SELECT t1.EmpNo
,t1.LastName
,t1.BirthDate
,t2.EmpNo
,t2.LastName
,t2.BirthDate
FROM Employee t1
INNER JOIN (
SELECT EmpNo
,LastName
,BirthDate
FROM Employee ) t2 ON t2.BirthDate = t1.BirthDate
WHERE t2.EmpNo != t1.EmpNo
你认为这是对的吗?
答案 0 :(得分:5)
我会这样做:
SELECT t1.EmpNo,
t1.LastName,
t1.BirthDate,
t2.EmpNo,
t2.LastName,
t2.Birthdate
FROM Employee t1, Employee t2
WHERE t1.BirthDate = t2.Birthdate
AND t1.EmpNo <> t2.EmpNo
答案 1 :(得分:1)
看起来不错但我会删除第二个选择.. 子查询
SELECT
t1.EmpNo ,t1.LastName ,t1.BirthDate ,t2.EmpNo ,t2.LastName ,t2.BirthDate
FROM Employee t1
INNER JOIN Employee t2 ON t2.BirthDate = t1.BirthDate
WHERE t2.EmpNo != t1.EmpNo
答案 2 :(得分:0)
我还会添加t1.EmpNo<t1.EmpNO
以避免重复的条件。
答案 3 :(得分:-1)
这是我的回答
Select name, date From employees Group by date of birth Having count(*)>1
;