Return 只有来自 table 的重复值

Return only duplicate values from table

我是 sql 的新手,我遇到了一个问题。 我有一个 table 的通话记录,其中包含两列 AnumberBnumber。如果有任何号码来电,将向 table 添加一个新行,其中 Anumber 作为来源,Bnumber 作为目的地。

我已经给出了两个 Anumber 值(3217913664,3006307180)现在我必须从 Bnumber(由 Anumber 调用)中找到所有值

假设我的 table 是:

ANUMBER        BNUMBER
-------        --------
3217913664     3006307180
3217913664     3212026005
3006307180     3212026005
3006307180     3212026007
3006307180     3212026008
3006307180     3212026009
3217913664     3212026009

现在我想从 Bnumber 中提取 value(3212026005 and 3212026009),因为给定的号码呼叫了这两个号码。所以我基本上只需要提取所有给定号码调用的号码。

我的英语不太好,但我想我已经解释了我的问题。知道如何实现这种情况吗?

这是一种方法:

select bnumber
from t
where anumber in (3217913664, 3006307180)
group by bnumber
having min(anumber) < max(anumber);

如果行没有重复项,则可以使用 count(*) = 2

如果要测试的 anumber 超过 2 个,则使用 count(distinct anumber) = n,其中 nin 列表中值的数量.

你可以试试这个:

select  Bnumber
from    yourtable
group by Bnumber
having COUNT(Bnumber) = (SELECT COUNT(*) FROM (SELECT DISTINCT Anumber FROM yourtable) AS T)

这是一个通用的。如果您在 table 中插入另一个数字,查询仍然有效。 只有一个警告:也许这不是解决您问题的最佳方法,但我现在无法考虑其他查询。也许以后我会尝试优化查询。