我对SQL很陌生,请多包涵。
假设我需要显示所有具有相同电话号码的员工ID(这两列在同一张表中)
我应该如何解决这个问题,使用内连接还是其他方法?
我对SQL很陌生,请多包涵。
假设我需要显示所有具有相同电话号码的员工ID(这两列在同一张表中)
我应该如何解决这个问题,使用内连接还是其他方法?
SELECT * FROM employees e1, employees e2
WHERE e1.phoneNumber = e2.phoneNumber
AND e1.id != e2.id;
更新:为了提高性能和加快查询速度,最好在 * 前面添加 e1
。
SELECT e1.* FROM employees e1, employees e2
WHERE e1.phoneNumber = e2.phoneNumber
AND e1.id != e2.id;
怎么样?
SELECT *
FROM Employees
WHERE PhoneNumber IN (
SELECT PhoneNumber
FROM Employees
GROUP BY PhoneNumber
HAVING COUNT(Employee_ID) > 1
)
JOIN
来完成此操作:SELECT *
FROM (SELECT *,COUNT(*) OVER(PARTITION BY phone_number) as Phone_CT
FROM YourTable
)sub
WHERE Phone_CT > 1
ORDER BY phone_number, employee_ids
演示:SQL Fiddle
select *
from Table1 as t1
where
exists (
select *
from Table1 as t2
where t2.Phone = t1.Phone and t2.id <> t1.id
)