Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6646147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:22:52+00:00 2026-05-26T00:22:52+00:00

I have a similar query as asked on this question MySQL – How to

  • 0

I have a similar query as asked on this question

MySQL – How to ORDER BY RELEVANCE? INNODB Table

Difference is, here I want to search from 5 fields as

add1, add2, add3, post_town, post_code

And only records in post_code field will be NOT EMPTY, other fields records may be empty at some places. If I search for keyword kingston, it returns

Acre Road, Kingston upon Thames, KT2 6EA

Kingston Road, Epsom, KT19 0DG

Kingston Road, Epsom, KT20 0DH

and these results are combination of all fields add1, add2, add3, post_town, post_code

I need this result in following order

Kingston Road, Epsom, KT19 0DG

Kingston Road, Epsom, KT20 0DH

Acre Road, Kingston upon Thames, KT2 6EA

My Current SQL query is like this

SELECT add1, add2, add3, post_town, post_code FROM address_mst 
WHERE add1 LIKE '%".$keyword."%'
OR add2 LIKE '%".$keyword."%'
OR add3 LIKE '%".$keyword."%'
OR post_town LIKE '%".$keyword."%'
OR post_code LIKE '%".$keyword."%'

So I need records with search Keyword coming in the beginning would come first. How can I do that ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:22:52+00:00Added an answer on May 26, 2026 at 12:22 am

    Not the prettiest but should work!

    select a.* from (
    SELECT add1, add2, add3, post_town, post_code, 1 as rank FROM address_mst 
    WHERE add1 LIKE '%".$keyword."%'
    UNION
    SELECT add1, add2, add3, post_town, post_code, 2 as rank FROM address_mst 
    WHERE add2 LIKE '%".$keyword."%'
    UNION
    SELECT add1, add2, add3, post_town, post_code, 3 as rank FROM address_mst 
    WHERE add3 LIKE '%".$keyword."%'
    UNION
    SELECT add1, add2, add3, post_town, post_code, 4 as rank FROM address_mst 
    WHERE post_town LIKE '%".$keyword."%'
    UNION
    SELECT add1, add2, add3, post_town, post_code, 5 as rank FROM address_mst 
    WHERE post_code LIKE '%".$keyword."%'
    ) a
    order by a.rank asc;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-table query, similar to this (simplified version) SELECT columns, count(table2.rev_id) As
Various incarnations of this question have been asked here before, but I thought I'd
This problem is similar to my previously asked question. When I query data using
I have seen a similar query here But I want to use XNA framework
I know similar permutations of this question have been asked before, but the answers
Ok, I have previously asked a similar question to this but it was voted
We have a Union Query. Here's a basic (similar) example: SELECT a.Name, b.Info FROM
I have a similar query to the one posted here . I create a
I asked a similar question a few weeks ago, but now the requirements have
I have asked a similar question 6 months ago that still has not been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.