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.
4
Answers
The reason why you don’t get anything is because you’re trying to evaluate
pizza = 'végétarienne'
andpizza = 'mexicaine'
at the same time. When a pizza is both vegetarian and mexicane? Never.That’s why you would require the
OR
condition here, to make sure either one of them is retrieved:In this case you’d retrieve people that eat either one of them, or both. If you want to restrict it to the people that like both and not only one, then you can look for people that like vegetarian, people that like mexicaine and intersect the two sets using the
INTERSECT
operation as follows:Your goal is not 100% clear to me. My first impression was you are just looking for an
IN
clause to fetch both pizzas, ‘végétarienne’ and ‘mexicaine’:An
IN
clause will fetch all entries having one of its values.Since a pizza can’t be "végétarienne" and "mexicaine" the same time, we can’t build this up using
AND
, but need anIN
clause orOR
.But at second glance, paying attention to your requirement to show the customers who eat both the "mexicaine" and the "végétarienne", this would be an incorrect approach because it would also list such customers who eat only one of them.
So if you are only looking for those customers who eat both pizzas, this can be done this way, using two
IN
clauses:A
WHERE
clause is a Boolean expression that each row is tested against. Only if a row returnsTRUE
for the whole expression does the row get returned.So, a
'végétarienne'
row would be tested by yourWHERE
clause and yieldTRUE AND FALSE
, which according to Boolean logic yieldsFALSE
.Similarly, a
'mexicaine'
row would be tested by yourWHERE
clause and yieldFALSE AND TRUE
, which according to Boolean logic also yieldsFALSE
.Note: Because no individual row is ever both
'végétarienne' AND 'mexicaine'
, no row will ever be returned by yourWHERE
clause.You could either use
OR
, or useIN()
. I’d recommend the latter…Or…
or use set based logic