mardi 12 juillet 2016

How to select mysql rows where condition is stored in a table?

I have a table which stores states of user search. It looks like this:

id | user_id | option | condition | value
------------------------------------------
 4 |       2 | price  | >=        | 300000
 5 |       2 | price  | <=        | 900000
 6 |       4 | status | =         | 1     
 7 |       4 | type   | =         | 1     
 8 |       5 | price  | >=        | 100000
 9 |       5 | price  | <=        | 125000
10 |       5 | status | =         | 2     
11 |       5 | type   | >=        | 2     
13 |       6 | price  | >=        | 200000
14 |       7 | price  | >=        | 500000
18 |       8 | price  | <=        | 600000
------------------------------------------

Now, I need to get all user_id where price value of 400000 fits any condition range (per user), OR status matches 1.

So, for this query, the result should be 2,4,6,8.

Any suggestions are welcome!

UPD: http://sqlfiddle.com/#!9/ea73a4/1

Aucun commentaire:

Enregistrer un commentaire