SQL 使用 Oracle 11G 进行 SELF-JOIN - 查找具有相同电话号码的患者
SQL SELF-JOIN with Oracle 11G - Finding a patient who has the same telephone number
我的 select 查询卡在了某个点。我需要从 table 'patient' 中找到一个 cellphone 号码,该号码与其他患者的 cellphone 号码具有匹配对(如果这有意义的话)。
此刻我从 table 患者那里得到了这个插入值:
insert into patient
values(1022010201, 88.2, 77, 0676762516);
insert into patient
values(1022010202, 66.7, 55, 0676762518);
insert into patient
values(1022010203, 59.6, 65, 0676762517);
insert into patient
values(1022010204, 99.1, 76, 0676762515);
insert into patient
values(1022010205, 88.2, 89, 0676762514);
insert into patient
values(1022010207, 91.4, 76, 0676762513);
insert into patient
values(182704726, 54.4, 44, 0676762516);
第一个和最后一个电话phone号码相同。通过我的 'self-join' 查询,我没有得到那个值,但我也得到了其他值。
在某种程度上它是有道理的,因为我的查询将 table 患者与其自身联系起来。所以通过这种方式 select 查询确实找到了匹配对的数字。
所以在这一点上我不知道如何只获得真正匹配的 phone 数字而不是所有其他数字。
this is my select query at the moment:
select p1.telefoonnr, p2.telefoonnr
from patient p1, patient p2
where p1.telefoonnr = p2.telefoonnr;
这是结果:
TELEFOONNR TELEFOONNR
-------------------- --------------------
676762516 676762516
676762518 676762518
676762517 676762517
676762515 676762515
676762514 676762514
676762513 676762513
06-12345678 06-12345678
7 rows selected
任何帮助都会很棒。
提前致谢
试试这个:
select telefoonnr,count(telefoonnr)
from patient
group by telefoonnr
having count (telefoonnr) > 1;
这就像在 table 中查找重复项。因此,上述查询将为您提供 telefoonnr
以及 count
和来自 table.
的其他详细信息
我找到了我自己问题的答案。
我要找的结果是这样的:
TELEFOONNR BSN
-------------------- ----------
676762516 1022010201
676762516 182704726
这可能是由于以下查询:
select telefoonnr, bsn
from patient
where telefoonnr =
(select telefoonnr
from patient
group by telefoonnr
having count(*) >1);
如果您想要共享手机号 和 他们共享的号码的患者,则自加入可以帮助您:
Select p1.ID, p2.ID, p1.telefoonnr
from Patient p1
join Patient p2
on p2.telefoonnr = p1.telefoonnr
and p2.ID <> p1.ID;
我的 select 查询卡在了某个点。我需要从 table 'patient' 中找到一个 cellphone 号码,该号码与其他患者的 cellphone 号码具有匹配对(如果这有意义的话)。
此刻我从 table 患者那里得到了这个插入值:
insert into patient
values(1022010201, 88.2, 77, 0676762516);
insert into patient
values(1022010202, 66.7, 55, 0676762518);
insert into patient
values(1022010203, 59.6, 65, 0676762517);
insert into patient
values(1022010204, 99.1, 76, 0676762515);
insert into patient
values(1022010205, 88.2, 89, 0676762514);
insert into patient
values(1022010207, 91.4, 76, 0676762513);
insert into patient
values(182704726, 54.4, 44, 0676762516);
第一个和最后一个电话phone号码相同。通过我的 'self-join' 查询,我没有得到那个值,但我也得到了其他值。
在某种程度上它是有道理的,因为我的查询将 table 患者与其自身联系起来。所以通过这种方式 select 查询确实找到了匹配对的数字。
所以在这一点上我不知道如何只获得真正匹配的 phone 数字而不是所有其他数字。
this is my select query at the moment:
select p1.telefoonnr, p2.telefoonnr
from patient p1, patient p2
where p1.telefoonnr = p2.telefoonnr;
这是结果:
TELEFOONNR TELEFOONNR
-------------------- --------------------
676762516 676762516
676762518 676762518
676762517 676762517
676762515 676762515
676762514 676762514
676762513 676762513
06-12345678 06-12345678
7 rows selected
任何帮助都会很棒。
提前致谢
试试这个:
select telefoonnr,count(telefoonnr)
from patient
group by telefoonnr
having count (telefoonnr) > 1;
这就像在 table 中查找重复项。因此,上述查询将为您提供 telefoonnr
以及 count
和来自 table.
我找到了我自己问题的答案。
我要找的结果是这样的:
TELEFOONNR BSN
-------------------- ----------
676762516 1022010201
676762516 182704726
这可能是由于以下查询:
select telefoonnr, bsn
from patient
where telefoonnr =
(select telefoonnr
from patient
group by telefoonnr
having count(*) >1);
如果您想要共享手机号 和 他们共享的号码的患者,则自加入可以帮助您:
Select p1.ID, p2.ID, p1.telefoonnr
from Patient p1
join Patient p2
on p2.telefoonnr = p1.telefoonnr
and p2.ID <> p1.ID;