Reputation: 133
I have a table in MySQL named sale with 3 columns like below : sale:
id|customers|type
I want to select sales that has type!=2 OR sales that has type=2 and customers!=0
I write statement like this :
sale.type != 2 OR (sale.type = 2 AND sale.customers != 0 )
but it doesn`t give me correct rows .
Also I have to use AND for other columns in this query but the operators between all of them is AND , just here I use OR .
Upvotes: 0
Views: 77
Reputation: 425063
Just remember thus rule: bracket the OR!
where foo = 'bar'
and (sale.type != 2 OR (sale.type = 2 AND sale.customers != 0 ) )
and blah = 3
Actually your condition can be simplified to:
( sale.type != 2 OR sale.customers != 0 )
Because logically checking for sale.type = 2 is redundant.
Upvotes: 1
Reputation: 16086
Just I try with this i found working: sqlfiddle
select * from sale where
sale.type != 2 OR (sale.type = 2 AND sale.customers != 0 )
Upvotes: 0
Reputation: 416
do you want sales.type not equal to 2 or (sales.type equal to 2 when you have customers not equal to 0?) you can use HAVING and WHERE together. Select * from sales where type !=2 or (type=2 HAVING customers !=0);
Upvotes: 0