[英]Finding similar records in a table
In a table with employees, I have a situation where for some records a similar record was created that differs by the country of birth (for one record it should always be the United States, for another it will always be a different country).在有员工的表中,我遇到了这样一种情况,对于某些记录,创建的类似记录因出生国家/地区而异(对于一个记录,它应该始终是美国,对于另一个记录,它始终是一个不同的国家/地区)。 For example:
例如:
ID ![]() |
name![]() |
surname![]() |
date_of_birth![]() |
country_of_birth![]() |
department_id![]() |
---|---|---|---|---|---|
1 ![]() |
Anna![]() |
Smith![]() |
11/01/1969 ![]() |
United States![]() |
1 ![]() |
177 ![]() |
Anna![]() |
Smith![]() |
11/01/1969 ![]() |
Argentina![]() |
1 ![]() |
I would like to find the name of such employee who has such records.我想查找拥有此类记录的此类员工的姓名。 I have tried such SQL (which is bad), but it does not return me any records.
我试过这样的 SQL (这很糟糕),但它没有返回任何记录。
Select e1.name,e1.surname from employees.employee e1, employees.employee e2
where e1.name = e2.name
and e1.surname = e2.surname
and e1.country_of_birth <> e2.country_of_birth
Please give me a hint on how to write the query properly请给我一个关于如何正确编写查询的提示
Use cross join without condition and use departament_id if you need it.如果需要,请使用无条件交叉连接并使用department_id。 Also you have a typo on the conditions.
此外,您的条件有误。 And you would know the Id's
你会知道我的身份
Select e1.Id, e2.Id, e1.name,e1.surname from employees.employee e1
cross join employees.employee e2
where e1.name = e2.name
and e1.surname = e2.surname
and e1.country_of_birth <> e2.country_of_birth
and e1.departament_id = e2.departament_id
If you wanna filter only those case that you know are only outside of US:如果您只想过滤那些您知道仅在美国境外的案例:
Select e1.Id, e2.Id, e1.name,e1.surname from employees.employee e1
cross join employees.employee e2
where e1.name = e2.name
and e1.surname = e2.surname
and e1.country_of_birth <> e2.country_of_birth
and e1.departament_id = e2.departament_id
and e2.country_of_birth <> 'United States'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.