我有一个表并且有很多数据。memid和其他列之间的关系是memid与其他列是一对一的。但是,我想查找同一介体可能具有不同的名字,姓氏等的情况。我的预期输出如下:
这里的memid 1003,10008与不同的memfirstname和memlastname关联。因此,我尝试如下操作:
select memid from ot.emp group by memid having count(distinct memfirstname)>1;
select memid from ot.emp group by memid having count(distinct memlastname)>1;
select * from ot.emp where memid in ('1003','10008');
我通过上面的查询得到了上面的输出,但是我需要使用两个不同的查询。我不能使用(distinct memfirstname,memlastname)
吗?有没有更好的查询来获取一对多映射的结果?
您可以使用exists
:
select e.*
from ot.emp e
where exists (select 1
from ot.emp e1
where e1.memid = e.memid and
(e.memfirstname <> e1.memfirstname or
e.memlastname <> e1.memlastname
)
);
如果希望memid
两者(memfirstname
&memlastname
)相同,则在子查询中使用AND
代替OR
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句