Search code examples
sqlpostgresqlwhere-clause

how to filter a table with where


I'm trying to filter my table 'mange' with the condition of customers who eat both the "mexicaine" and the "végétarienne".

I have a table 'mange' which contains 'nom' and 'pizza'

I have tried to filter with

select *
from mange
where ( pizza = 'végétarienne')
and ( pizza = 'mexicaine')

But I don't get anything even though I have some 'nom' who eat both.


Solution

  • SELECT nom, count(distinct pizza) as DistinctpizzaCNt
    FROM mange
    WHERE ( pizza in 'végétarienne', 'mexicaine')
    GROUP BY nom
    having count(distinct pizza) = 2 
    

    or use set based logic

    SELECT nom
    FROM mange
    WHERE ( pizza = 'végétarienne')
    
    INTERSECT
    
    SELECT nom
    from Mannge
    WHERE ( pizza = 'mexicaine')