Skip to main content
Inspiring
March 11, 2008
Question

order search results

  • March 11, 2008
  • 3 replies
  • 458 views
Hi there,

I've currently got an area search via postcode. The user enters their postcode e.g. LS6 1ER and the search then finds everything in 'LS'.

The results and ordered by postcode however obviously are returned like LS1, LS2, LS3 etc.

I want the results of LS6 to appear first, then LS5, LS4 etc...

Any ideas how i can do this?

Cheers

James
    This topic has been closed for replies.

    3 replies

    Inspiring
    March 11, 2008
    I understand it differently. If the db supports this syntax, you're in business

    select case when postcode not like '#searchstring#%' then 0
    else 1 end sortby, postcode, etc
    dot dot dot
    order by sorty, postcode desc
    RookOrnAuthor
    Inspiring
    March 11, 2008
    yea ive got that already but that doesnt order by the search term first.

    If the search term is LS6 i want the LS6's to be first, LS5 second, LS4 third etc...
    Inspiring
    March 11, 2008
    RookOrn,

    If I understand correctly you want to order by postcode if postcode is searched and other fields if another field is searched. See attached code for sample that creates WHERE and ORDER BY based on search parameters.

    Be aware that care should be taken to validate input due to the risk of SQL injection when creating queries dynamically like this.

    If I am not understanding your request, please post your code and explain further.




    Participating Frequently
    March 11, 2008
    If you are looking for a just simple descending sort order......

    ORDER BY postcode DESC

    ..in your query

    Phil