Canman2005 Posted January 30, 2008 Share Posted January 30, 2008 Hi all Im currently running a complex query and I need to try and sort the results into an order which the closest match first. I was told there is a simple ORDER BY function which can be used. Has anyone done a ORDER BY CLOSEST MATCH or whatever the query should be? Any help would be sweet Thanks Ed Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/ Share on other sites More sharing options...
trq Posted January 30, 2008 Share Posted January 30, 2008 The ORDER BY clause works by sorting on a particular field and naturally sorts in either descending or ascending. You'll need to explain a little clearer what it is exactly your trying to do. Maybe show us some example data? Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-453600 Share on other sites More sharing options...
effigy Posted January 30, 2008 Share Posted January 30, 2008 Full-text searches can be ordered by relevancy. Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-453609 Share on other sites More sharing options...
sasa Posted January 30, 2008 Share Posted January 30, 2008 you look closest match define distanc pleas Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-453704 Share on other sites More sharing options...
Canman2005 Posted January 31, 2008 Author Share Posted January 31, 2008 Hi Well my query is the following, it basically takes keywords entered and searches the database for anything matching, I need to get these to order with the closest match showing first. Is that possible? $arrSearches = explode(" ",$_GET['q']); $arrMatches = array(); foreach ($arrSearches as $strKey => $strValue) { array_push($arrMatches," `title` like '%$strValue%' "); } $remakequery = implode(" OR ", $arrMatches); $sql = "SELECT * FROM news WHERE ".$remakequery.""; print $sql.'<br>'; $query = @mysql_query($sql,$connection) or die(mysql_error()); while ($row = mysql_fetch_array($query)) { print $row['title'].'<br>'; } Any help would be ace as ive been trying to crack this for days now. Thanks Ed Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-454637 Share on other sites More sharing options...
effigy Posted January 31, 2008 Share Posted January 31, 2008 How do you define "closest match" when you're using a wildcard? Are you saying that "keyword abc" is closer than "keyword abc def" because it has less content? Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-454657 Share on other sites More sharing options...
Canman2005 Posted January 31, 2008 Author Share Posted January 31, 2008 Hi Almost work out a % based on the best match and order them by that. Does make any sense? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-454674 Share on other sites More sharing options...
Canman2005 Posted January 31, 2008 Author Share Posted January 31, 2008 i'm trying to order it much like they do on this site http://www.phpfreaks.com/forums/index.php?action=search;advanced see there order by? Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-454677 Share on other sites More sharing options...
effigy Posted January 31, 2008 Share Posted January 31, 2008 Linking searches does not work. Are you talking about the relevance column? If so, I don't think this is possible without using a full-text search, which I mentioned previously. Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-454684 Share on other sites More sharing options...
Canman2005 Posted January 31, 2008 Author Share Posted January 31, 2008 sorry, yes the relevance column Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-454694 Share on other sites More sharing options...
Canman2005 Posted January 31, 2008 Author Share Posted January 31, 2008 anyone got any bright ideas? Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-454858 Share on other sites More sharing options...
effigy Posted February 1, 2008 Share Posted February 1, 2008 Full-text searches can be ordered by relevancy. Quote Link to comment https://forums.phpfreaks.com/topic/88596-order-by-closest-match/#findComment-455370 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.