简体   繁体   English

SQL。可以吗?

[英]SQL.. Is it possible?

SQLite with python. 带有python的SQLite。

column1 |  column2    |
a       |      1      |
a       |      2      |
a       |      3      |
b       |      1      |
b       |      2      |
b       |      3      |
c       |      1      |
c       |      2      |
d       |      1      |

Is it possible to do something like this 是否可以做这样的事情

I need to take column1's element name which has column2's 1,2,3 all. 我需要采用column2的1,2,3全部的column1的元素名称。 so It should be [a, b] 所以应该是[a,b]

With python, I usually use to select some. 使用python时,我通常会选择一些。

cur.execute('''SELECT DISTINCT column1 from tablename where column2 = ? and column1 = ?''',("0", "a",))

print(cur.fetchall())

As you know, It is not related much with the problem. 如您所知,它与问题关系不大。

You can use group by and having . 您可以使用group byhaving Assuming no duplicates: 假设没有重复:

select column1
from t
where column2 in (1, 2, 3)
group by column1
having count(*) = 3;

You can try following: 您可以尝试以下操作:

select column1
from t
where column2 in (1, 2, 3)
group by column1
having count(distinct col2) = 3;

Cheers!! 干杯!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM