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 8091055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:56:09+00:00 2026-06-05T19:56:09+00:00

I need to search for the closest Dialcode bigger than the actual one. For

  • 0

I need to search for the closest Dialcode bigger than the actual one. For example the closest from 33 will be 331 or 332 (if 331 doesn’t exist)… So it has to be 33xxxx, 34 is not valid.

These 2 queries work but are too slow (250ms/row):

SELECT Dialcode
FROM table
WHERE (Dialcode LIKE '$var%' AND Dialcode > '$var' AND Price IS NOT NULL)
ORDER BY Dialcode ASC LIMIT 1

SELECT Dialcode
FROM table
WHERE (MATCH(Dialcode) AGAINST ('$var*' IN BOOLEAN MODE) AND Dialcode > '$var'
      AND Price IS NOT NULL)
ORDER BY Dialcode ASC LIMIT 1

My Dialcode is a PRIMARY KEY BIGINT(15).

Is I do this, it’s really fast (>1ms/row) but it’s not exactly what I need:

SELECT Dialcode
FROM table
WHERE (Dialcode >= '$var' AND Price IS NOT NULL)
ORDER BY Dialcode ASC LIMIT 1

So I guess my problem is the LIKE / MATCH AGAINST.

Any ideas would be appreciated.

UPDATE solution:

Solution adapted from raina77ow suggestion:

SELECT Dialcode FROM table WHERE (( (Dialcode BETWEEN $var * 1 AND ’9’ )
   OR (Dialcode BETWEEN $var * 10 AND $var.’99’ )   
   OR (Dialcode BETWEEN $var * 100 AND $var.’999’ )
OR (Dialcode BETWEEN $var * 1000 AND $var.’9999’ )
…
) AND Price IS NOT NULL) ORDER BY Dialcode ASC LIMIT 1

Thanks guys!

  • 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-06-05T19:56:10+00:00Added an answer on June 5, 2026 at 7:56 pm

    I see that the main problem here is that index is built upon the numeric values of Dialcode – not the string ones. So both queries do not use index at all.

    You can try to build a numeric function, the problem is that this function will have Dialcode in the left part of evaluation – so the index again won’t be used.

    Perhaps this approach might be more useful.

    SELECT Dialcode 
    FROM table 
    WHERE ( (Dialcode BETWEEN %value% * 10 AND (%value%*10 + 9) 
       OR (Dialcode BETWEEN %value% * 100 AND (%value%*100 + 99) 
       OR (Dialcode BETWEEN %value% * 1000 AND (%value%*1000 + 999)
       ...
    ) AND Price IS NOT NULL
    ORDER BY Dialcode LIMIT 1;
    

    It’s ugly as hell (I even wrote this wrong the first two times – alfasin should get the credit for correcting me), yes, but it should hit the index. The alternative is using UNION (and not OR), but I suppose both queries will be executed the same way (it’s not verified, though).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need simple search-replace in doc file exported from google doc. and than upload
I need to search for a pattern in files. For example the content of
I need to search between dates and times. For example, between date: 30/02/2007, time:
I need to search throw a text field in mysql from my java application.
I need to search for all users containing a certain text string in their
I need to search a file for a word and return the whole line
I need to search a name which is stored in collection. Search criteria: eg:
I need to search some data depending on whether user enters a postcode or
I need to search for instances of a character sequence in a Java string
I need a search panel similar to the findpanel in devexpress. FindPanel is a

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.