how so select similarities in MySQL?
- by mysqllearner
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:
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
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?