how so select similarities in MySQL?

Posted by mysqllearner on Stack Overflow See other posts from Stack Overflow or by mysqllearner
Published on 2010-05-18T06:19:36Z Indexed on 2010/05/18 6:20 UTC
Read the original article Hit count: 215

Filed under:
|
|
|
|

Currently, I am doing a search function.

Lets say in my database, I have this data:

  • Keyword1
  • Keyword2
  • Keyword3
  • Keysomething
  • Key

and the user entered: "Key" as the keyword to search. This is my current query: Q1: SELECT * FROM data WHERE (data_string LIKE '$key%' OR data_string LIKE '%$key%' OR data_string LIKE '%$key')

Basically, I have 2 questions:

  1. How do I sort by (order by) similarity. From above example, I should have "Key" as my first result. My current result is: Keyword1, Keyword2, Keyword3, Keysomething and Key

  2. My SQL query only search by the "data_string" column, what if I want to seach others column? Do I need to do something like this:

    Q2: SELECT * FROM data WHERE (data_string LIKE '$key%' OR data_string LIKE '%$key%' OR data_string LIKE '%$key') OR (data_other LIKE '$key%' OR data_other LIKE '%$key%' OR data_other LIKE '%$key') ...

Is there any better/faster query than Q2?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about query