Jump to content

ORDER BY - Closest match


Canman2005

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.