Standard ORDER BY's doesnt work on my query - mysql

I Want to Order the search results by relevancy but standard techniques doesn't work on my code because it is doing double search check in the query to find the ones with 'Match' and 'Like' as well. How can I perform it with the code below;
$qry = mysql_query("
SELECT *
FROM workdb
WHERE publish = 'Yes'
AND MATCH (workname, keyword1, keyword2, keyword3) AGAINST ('*$searched*' IN BOOLEAN
MODE)
OR publish = 'Yes'
AND (workname LIKE '%$searched%'
OR keyword1 LIKE '%$searched%'
OR keyword2 LIKE '%$searched%'
OR keyword3 LIKE '%$searched%')
LIMIT $offset, $rowsperpage
");
EDIT: I want to display the result from most relevant to least relevant. The reason I use Math and LIKE both is to not miss any matched elements from the search.
EDIT2: OK lets make it clear, I have a workname and 3 keywords in database rows so I want to get the ones with the Best match to my searched text/word and put them into an order to most LIKE to least LIKE/ Most matches to Least Matches.

The answer is here if anyone have the same problem. Nobody understand me instead they downvote.
mysql_query("SELECT *,
(MATCH (workname) AGAINST ('*$searched*' IN BOOLEAN MODE)
AND
(workname LIKE '%$searched%'))
AS relevancy
FROM workdb
WHERE publish='Yes'
AND
(MATCH (workname, keyword1, keyword2, keyword3) AGAINST ('*$searched*' IN BOOLEAN MODE)
OR
(workname LIKE '%$searched%' OR keyword1 LIKE '%$searched%' OR keyword2 LIKE '%$searched%' OR keyword3 LIKE '%$searched%'))
ORDER BY relevancy DESC
LIMIT $offset, $rowsperpage");

Related

SQL Query AND statement not returning correct date based on search results [duplicate]

SELECT * FROM people
WHERE
university='2'
AND MATCH (lname,fname) AGAINST ('+massive' IN BOOLEAN MODE)
OR (fname LIKE '%box%' OR lname LIKE '%box%')
This query is allowing results to filter through other than those of university='2' how would I update this so it strictly only shows results where university = 2
The reason I have combined fulltext search with LIKE is because of the minimum letter count that full text search has and because I am on a shared hosting plan I am unable to modify the settings. As a result I have combined both full text and LIKE in order to accommodate
Fix your parentheses
SELECT * FROM people
WHERE
university='2'
AND (MATCH (lname,fname) AGAINST ('+massive' IN BOOLEAN MODE)
OR fname LIKE '%box%'
OR lname LIKE '%box%')
AND has higher precedence than OR, so university = '2' was only being combined with MATCH, not with the fname/lname tests.

match against, sort by relevance

My Query
$query = selectQuery('SELECT * FROM page_info WHERE MATCH(content)
AGAINST("reactie klanten" WITH QUERY EXPANSION)');
how can I sort by relevance? i get back about 10 rows, but the row that actually contains the words 'reactie' and 'klanten' is somewhere in the middle. the rest of the results is somehow relevant with the words :S
MySQL documentation about 'WITH QUERY EXPANSION'
EDIT
changed by query to:
SELECT *, MATCH(content) AGAINST("reactie klanten") AS relevance FROM page_info WHERE MATCH(content) AGAINST("reactie klanten" WITH QUERY EXPANSION) ORDER BY relevance DESC
this seems promising, because now the field is on top of the list.
still how are the others related?
example:
$text = This page is being tested by our tester
how is $text relevant to reactie klanten?

mysql match against multiple words

Below is an example sql I am stuck with, it will not return a product named "iphone 4s", It returns 10 other result. Any help would be great thanks
1st sql example
SELECT * FROM products
WHERE match(desc) against('+iphone +4s' IN BOOLEAN MODE) LIMIT 10";
result: contains the words 'iphone' and '4s'
2nd sql example
SELECT * FROM products
WHERE match(desc) against('+iphone 4s' IN BOOLEAN MODE) LIMIT 10";
result: contains the words 'iphone', but rank rows higher if they also contain '4s'
3rd sql example
SELECT * FROM products
WHERE match(desc) against('iphone 4s' IN BOOLEAN MODE) LIMIT 10";
result: contains the words 'iphone' or '4s'
What I want to search is 'iphone 4s', but it comes with other result, like 'iphone is good, but 4s...', 'new iphone 5 has published...',...
Can anyone help me to solve it? thanks.
To match an exact phrase, just use double quotes to surround the phrase to match;
SELECT *
FROM products
WHERE MATCH(desc)
AGAINST('"iphone 4s"' IN BOOLEAN MODE)
LIMIT 10
More info at the manual pages.
Use REGEXP
SELECT * FROM products
WHERE desc REGEXP 'iphone[[. .]]*4s'
LIMIT 10;
SQLFiddle demo

mysql multiple OR NOT LIKES

I have a wordpress plugin that essentially creates a mysql query and returns the results to wordpress.
It is user driven and so can end up in large queries with multiple NOT LIKEs which results in a very slow query.
Any suggestions that I could use to improve:
SELECT field1,field2,field3,field4
from datatable
WHERE (title NOT LIKE '%word%' AND title NOT LIKE '%word2%'
AND title NOT LIKE '%word3%' AND title NOT LIKE '%word4%'
AND title NOT LIKE '%word5%' AND title NOT LIKE '%word6%'
AND title NOT LIKE '%word7%' AND title NOT LIKE '%word8%'
AND title NOT LIKE '%word9%')
AND MATCH (title) AGAINST ("\"brandname\" " IN BOOLEAN MODE)
ORDER BY total ASC LIMIT 0,60
The customer is adding a lot of negative keywords to the wordpress plugin which results in larger queries than the one above.
This is most easily done with REGEXP. For multiple words, use a group like (one|two|three)
SELECT
field1,
field2,
field3,
field4
from datatable
WHERE
title NOT REGEXP '(word1|word2|word3|word4|word5...|word9)'
AND MATCH (title) AGAINST ("\"brandname\" " IN BOOLEAN MODE)
ORDER BY total ASC
LIMIT 0,60
You can use a REGEXP operation to compare all the patterns at once.
Your query will be something like:
SELECT field1,field2,field3,field4
FROM data table
WHERE title NOT REGEXP '^word[0-9]?$'
AND MATCH(title) ("\"brandname\" " IN BOOLEAN MODE)
ORDER BY total ASC LIMIT 0,60

MySQL: How to do basic search by relevance?

I read the official page: http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html
but it's not providing an example
I have 1 col named "mycol", and suppose user input is "keyword1 keyword2". I want to display results ordered by relevance. I tried something like this:
select id,mycol,match(mycol) against('keyword1 keyword2' in boolean mode) as relevance from mytable
It's returning all records relevance is 0. What am I doing wrong?
try
select id,mycol,match(mycol) against('keyword1 keyword2') as relevance from mytable order by relevance desc