Skip to content
Advertisement

SQL.. Is it possible?

SQLite with python.

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. so It should be [a, b]

With python, I usually use to select some.

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.

Advertisement

Answer

You can try following:

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

Cheers!!

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement