简体   繁体   English

检索 a 列值,其中主键是元组 (a,b) 并且对于 b' 值的给定列表必须存在所有行 (a,b')

[英]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)列表和本人需要在存在的类型的每一个值“对于bBVALs行条目(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 byhaving

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.

相关问题 Select 行,其中 A 列的值以 B 列的值开始 - Select rows where value of column A starts with value of column B 如果 B[i, j] == 1,则从 A 创建包含每列 A 行平均值的新矩阵,其中 B 是邻接矩阵 - Creation of new matrix from A containing the average value of A rows for each column if B[i, j] == 1 where B is an adjacency matrix 如果 B[i, j] == 1 其中 B 是邻接矩阵,则从 A 生成包含每列 A 行平均值的新矩阵 - Generate new matrix from A containing the average value of A rows for each column if B[i, j] == 1 where B is an adjacency matrix 如何删除 Pandas DF 中的行,其中 A 列的值位于 B 列和 C 列匹配 - How to remove rows in Pandas DF where value from Column A is in Column B and Column C Match Pandas Dataframe Groupby 其中 A 列值 == B 列值 - Pandas Dataframe Groupby where column A value == Column B value 如何替换 A 列中 B 列等于值的值? - How to replace a value in column A where column B is equal to value? Python:如果我有一个元组 (A,B),如何根据 A 值取范围内所有 B 值的平均值? - Python: If I have a tuple (A,B), how do I take the mean all B values in ranges based on A value? 更新Pandas DataFrame中B列的值为C的A列 - Update Column A where Column B has Value C in Pandas DataFrame Python:过滤列表 A 包含在列表 B 中的值 - Python: Filter for Values where List A is Contained within List B 在列表A中找到第一个索引,其中值在列表B中 - Find the first index in a list A where the value is in a list B
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM