Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

order search results

Explorer ,
Mar 11, 2008 Mar 11, 2008
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
391
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Mar 11, 2008 Mar 11, 2008
If you are looking for a just simple descending sort order......

ORDER BY postcode DESC

..in your query

Phil
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 11, 2008 Mar 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...
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Mar 11, 2008 Mar 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.




Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 11, 2008 Mar 11, 2008
LATEST
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources