So, I'm working on a simple search page on my website and I'm trying to select only the most relevant information.
In order to do that, I'd like to select data from 2 different tables. But I'd only like to select data that is matched to the search query.
Now, here's the fun part. I'd only like to select the data with the most occurrences of $variable and then sort that data from highest to lowest and then limit the amount of results(to something 1,000 or 2,000)
To ask it in lesser words/code form. I'd like to do something like this in SQL(run by PHP)
$query = ["search","me","please"];
$maxData = 1500;
foreach($query as $value){
//SQL start(Part I need help on)
$res1 = SELECT * from table1 WHERE column LIKE %$value% AND return columns with highest times $value appears LIMIT $maxData
$res2 = SELECT * from table2 WHERE column1,column2,column3 LIKE %$value% AND return columns with highest times $value appears LIMIT $maxData
}
I'd like it to count how many times the searched variable appears in the column and return the times in order from highest count to lowest
Is all of this possible in SQL alone?
Also, if my question is unclear or hard to understand, Please say so.
you can use mysql fulltext
search. in this case you will get a score per result.
See more details here
It can be done using sql by using group by statement and by using of a helper table.
Create table tbl_search with one column search_term as varchar.
This table will be truncated and refill with search terms each time the query is executed.
Select * from
(Select search_term, count(*) as hits
from table1 , tbl_search
where table1.column like '%' + tbl_search.search_term +'%'
Group by tbl_search.search_term) as z1
Order by hits
Index the tbl_search.search_term to help query performance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.