仅选择具有匹配列表列值的一行

时间:2019-10-05 16:37:36

标签: mysql

我有一个表结构

In [16]: df = pd.DataFrame({"col":["a", "b", "xyz"]})

In [17]: df["encoded"] = df["col"].apply(lambda x: x.encode().hex())

In [18]: df["decoded"] = df["encoded"].str.decode("hex")

In [19]: df
Out[19]:
   col encoded decoded
0    a      61    b'a'
1    b      62    b'b'
2  xyz  78797a  b'xyz'

In [22]: df["decoded"] = df["encoded"].apply(lambda x: bytes.fromhex(x).decode())

In [23]: df
Out[23]:
   col encoded decoded
0    a      61       a
1    b      62       b
2  xyz  78797a     xyz

In [25]: df["decoded"] = df["encoded"].str.decode("hex").str.decode("utf-8")

In [26]: df
Out[26]:
   col encoded decoded
0    a      61       a
1    b      62       b
2  xyz  78797a     xyz

如何获得条件为colA | colB -------------- 1 | A 1 | B 1 | C 2 | X 2 | B 2 | C 3 | Y 3 | B 3 | X 的{​​{1}}?我尝试使用此colA return = 2,但结果是WHERE colB are X, B, C

1 个答案:

答案 0 :(得分:0)

考虑使用聚合:

select a
from mytable 
group by a
having
    sum(b = 'B') >= 1
    and sum(b = 'C') >= 1
    and sum(b = 'X') >= 1

这将为您提供至少每条存在的记录,其中b列的值为“ B”,一个记录为“ C”,一个记录为“ X”。

您可以对此设置更严格的限制,例如,如果您不希望b中除了'A','B','X'之外的其他值,则表示:

select a
from mytable 
group by a
having
    sum(b = 'B') = 1
    and sum(b = 'C') = 1
    and sum(b = 'X') = 1
    and count(*) = 3