[英]Retrieving the a column value where the primary key is a tuple (a,b) and where all rows (a,b') must exist for a given list for b' values
I have a table of form A|B|C where a tuple (a,b) in (A,B) is the primary key.我有形式A的表| B | C,其中在(A,B)的元组(A,B)是主键。 I have list of values (BVALs) for B and I require the elements in the column A where a row entry for each value of the type (a,b') for b' in BVALs exists.
我有B的值(BVALs)列表和本人需要在存在的类型的每一个值“对于b在BVALs行条目(A,B)”的列A中的元素。
Currently I have implemented a script that retrieves the first all (a,b'') for the first element of the BVALs, which then iterates and refines the list until the last element of the BVALs.目前我已经实现了一个脚本,它检索 BVAL 的第一个元素的第一个 (a,b''),然后迭代并优化列表,直到 BVAL 的最后一个元素。 I believe it will be slow in big databases and believe that an faster solution exists.
我相信它在大型数据库中会很慢,并且相信存在更快的解决方案。 Would appreciate any help.
将不胜感激任何帮助。
Let's say we have the following table:假设我们有下表:
+---+---+
| A | B |
+---+---+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
+---+---+
If the BVALs list consists of (1,2) then the query should return 1 and 2如果 BVALs 列表由 (1,2) 组成,则查询应返回 1 和 2
I understand that you want a
s that have all b
values.我知道您想要
a
s 具有所有b
值。 If so, you can use group by
and having
:如果是这样,您可以使用
group by
并having
:
select a
from mytable
where b in (1, 2) -- either value
group by a
having count(*) = 2 -- both match
Is this what you want?这是你想要的吗?
select distinct a
from t
where b in ( . . . ); -- list of b values here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.