V
V
Viktor Vsk2014-06-18 13:38:52
MySQL
Viktor Vsk, 2014-06-18 13:38:52

Product filters WHERE AND or OR ?

Product filter. Products have an option type (for example, screen diagonal, processor frequency...), type has a value (15.6`, 17`, 3Gz, 2Gz...)
This is how I can select, for example, all Asus:

Product.where("
(option_types.id = 12 AND option_values.id IN (96,100))
")

Screen diagonal:
Product.where("
(option_types.id = 58 AND option_values.id IN (337,338,340,341))
")

But this:
Product.where("
(option_types.id = 12 AND option_values.id IN (96,100))
AND (option_types.id = 58 AND option_values.id IN (337,338,340,341))
")

Always gives 0 results (although individually they work correctly)
And this:
Product.where("
(option_types.id = 12 AND option_values.id IN (96,100))
OR (option_types.id = 58 AND option_values.id IN (337,338,340,341))
")

Gives undefined result. For example, if the first line selects all Asus, then in the case when there are two of them through OR, then other brands may appear.
I understand that filters work like this (when checkboxes are clicked, for example):
Inside the type, all products that match each property are selected (Asus is checked -> add all Asus to the selection ...)
There is an exception between types (That is, if Acer is selected , then - 5GZ, then subtract from the first sample those where not 5Gz ...)
It seems everything is simple, but I’m already confused and I don’t understand why the queries work correctly separately, and through AND (this is the usual logical AND?) - the result - 0.
PS Requests were written in general form, there were more joins before them, etc.
PSS I correctly understand that it is impossible to use inner join instead of where, because sampling - within one table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Penkovsky, 2014-06-18
@Able1991

AND - an operator that displays records if the first and second conditions are true / true
OR - an operator that displays records if at least one of the two conditions is true / true
so your approach is wrong
so think about your approach

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question