Sunday 15 August 2010

mysql - How to select a row by a certain amount of similar data -



mysql - How to select a row by a certain amount of similar data -

i'm build sort of function or query can check if record exists in database. next rules apply:

the table has 6 columns my yet-to-build-query has access finish row-object (all 6 values) this query should find each row @ to the lowest degree 4 out of 6 corresponding values object passed using mysql

is possible build query this? goal have function can homecoming true if it's row like passed object existing in database.

is alternative create query multiple where-statements (where seek for each combination 4 different values)?

pseudo:

function getsimilarrow(row_object $row) { //select * //from table_x //where 4 out of 6 properties object $row apply }

you utilize case statement in clause each property trying match. if meets criteria give case statement value of 1; if doesn't give 0. sum of cases should >= 4.

i'm not that familiar mysql next work (i knocked quick sql fiddle show working):

select * sometable (case when propertyone = 'value1' 1 else 0 end) + (case when propertytwo = 'value2' 1 else 0 end) + (case when propertythree = 'value3' 1 else 0 end) + (case when propertyfour = 'value4' 1 else 0 end) + (case when propertyfive = 'value5' 1 else 0 end) + (case when propertysix = 'value6' 1 else 0 end) >= 4

obviously alter logic in each clause if you'd prefer them likes or anything. apply weighting each column using other 1 if needed creative.

mysql sql where approximation

No comments:

Post a Comment