mysql query that takes sequence into account -


  • search term a: black small rock
  • search term b: black rock small
  • search term c: small rock black

i want design query take order in terms written account. i'd above 3 terms produce same results, want order of results reflect sequence of terms.

i hope makes sense; please let me know if doesn't.

the way can see working if separating search terms attributes, in database table, in specific order.

  • attribute 1: black
  • attribute 2: small
  • attribute 3: rock

in case, query first search term "black small rock" match "black" in attributes 1, 2, or 3 , perform same logical-or operation other 2 fields in search term.

your query, rather simple:

select * table (attr1 = 'black' or attr2 = 'black' or attr3 = 'black') , (attr1 = 'small' or attr2 = 'small' or attr3 = 'small') , (attr1 = 'rock' or attr2 = 'rock' or attr3 = 'rock'); 

at point, rely on logic in programming language issuing query order displayed data search term, test first attribute (python-like pseudocode):

search_terms = split(search_term, " ") ordered_results = [    [x x in results if x["attr1"] == search_terms[0]],    [y y in results if x["attr2"] == search_terms[1]],    [z z in results if z["attr3"] == search_terms[2]] ] result_set in ordered_results:    r in result_set:       # display result. 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -