我有一个用户表,可以像这样。其中Socialnr有一行或多行是空的。
Userid:1 Phone:12345 Email:[email protected] Socialnr:88776655
Userid:2 Phone:12345 Email: Socialnr:
Userid:3 Phone: Email:[email protected] Socialnr:
假设我只有从一开始就有Socialnr.所以我需要
select * from users where socialnr=88776655
但是,我需要能够选择另外两行,以及基于相同的电子邮件或电话号码,我从第一次选择.我需要单独的行.任何输入赞赏,谢谢。
根据Gordons的回答,我现在有了我的真实代码。
SELECT u.* FROM appmanager.appusers u where u.userId='797' and u.personnrvh2='88776655' OR exists (select 1 from appmanager.appusers u2 where u2.userId='797' and u2.personnrvh2='88776655' and (u2.emailvh2 = u.emailvh2 or u2.mobilvh2 = u.mobilvh2))
好了,这就是解决方案,非常感谢Gordon!
嗯......根据你的描述,你可以使用 exists
:
select u.*
from users u
where u.Socialnr = 88776655 or
exists (select 1
from users u2
where u2.Socialnr = 88776655 and
(u2.email = u.email or u2.phone = u.phone)
);
如果你愿意,你也可以使用窗口函数......。.假设每一个 email
和 phone
有一个最终 Socialnr
select u.*
from (select u.*,
max(Socialnr) over (partition by email) as imputed_socialnr_email,
max(Socialnr) over (partition by phone) as imputed_socialnr_phone
from users u
) u
where 88776655 in (Socialnr, imputed_socialnr_email, imputed_socialnr_phone)