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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:07:07+00:00 2026-05-15T07:07:07+00:00

I have a problem with a subquery involving IPV4 addresses stored in MySQL (MySQL

  • 0

I have a problem with a subquery involving IPV4 addresses stored in MySQL (MySQL 5.0).

The IP addresses are stored in two tables, both in network number format – e.g. the format output by MySQL’s INET_ATON(). The first table (‘events’) contains lots of rows with IP addresses associated with them, the second table (‘network_providers’) contains a list of provider information for given netblocks.

events table (~4,000,000 rows):

event_id (int)
event_name (varchar)
ip_address (unsigned int)

network_providers table (~60,000 rows):

ip_start (unsigned int)
ip_end  (unsigned int)
provider_name (varchar)

Simplified for the purposes of the problem I’m having, the goal is to create an export along the lines of:

event_id,event_name,ip_address,provider_name

If do a query along the lines of either of the following, I get the result I expect:

SELECT provider_name FROM network_providers WHERE INET_ATON('192.168.0.1') >= network_providers.ip_start ORDER BY network_providers.ip_start DESC LIMIT 1

SELECT provider_name FROM network_providers WHERE 3232235521 >= network_providers.ip_start ORDER BY network_providers.ip_start DESC LIMIT 1

That is to say, it returns the correct provider_name for whatever IP I look up (of course I’m not really using 192.168.0.1 in my queries).

However, when performing this same query as a subquery, in the following manner, it doesn’t yield the result I would expect:

SELECT 
events.event_id,
events.event_name,
    (SELECT provider_name FROM network_providers 
    WHERE events.ip_address >= network_providers.ip_start 
    ORDER BY network_providers.ip_start DESC LIMIT 1) as provider
FROM events

Instead the a different (incorrect) value for provider is returned. Over 90% (but curiously not all) values returned in the provider column contain the wrong provider information for that IP.

Using events.ip_address in a subquery just to echo out the value confirms it contains the value I’d expect and that the subquery can parse it. Replacing events.ip_address with an actual network number also works, just using it dynamically in the subquery in this manner that doesn’t work for me.

I suspect the problem is there is something fundamental and important about subqueries in MySQL that I don’t get. I’ve worked with IP addresses like this in MySQL quite a bit before, but haven’t previously done lookups for them using a subquery.

The question:

I’d really appreciate an example of how I could get the output I want, and if someone here knows, some enlightenment as to why what I’m doing doesn’t work so I can avoid making this mistake again.

Notes:

The actual real-world usage I’m trying to do is considerably more complicated (involving joining two or three tables). This is a simplified version, to avoid overly complicating the question.

Additionally, I know I’m not using a between on ip_start & ip_end – that’s intentional (the DB’s can be out of date, and such cases the owner in the DB is almost always in the next specified range and ‘best guess’ is fine in this context) however I’m grateful for any suggestions for improvement that relate to the question.

Efficiency is always nice, but in this case absolutely not essential – any help appreciated.

  • 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-15T07:07:08+00:00Added an answer on May 15, 2026 at 7:07 am

    You should take a look at this post:

    http://jcole.us/blog/archives/2007/11/24/on-efficiently-geo-referencing-ips-with-maxmind-geoip-and-mysql-gis/

    It has some nice ideas for working with IPs in queries very similar to yours.

    Another thing you should try is using a stored function instead of a sub-query. That would simplify your query as follows:

    SELECT 
    event.id,
    event.event_name,
    GET_PROVIDER_NAME(event.ip_address) as provider
    FROM events
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck in this problem for quite while now. I have two tables ItemMaster
My problem is the following: I have two tables; persons and teams, I want
I'm make a little game in php with mysql. Now I have a problem
I have two tables almost identical: employee employee_history The second table has two additional
I am having the following problem. I have to count the number of items
Im little speak english. I have an sql subquery error Database : MySQL Table
Here is my problem : I have 3 tables : account, account_event and account_subscription
i have a problem with this mysql query. It take vmore as 1 day
I have a question on making a good subquery for this problem Let's say
Here's my problem: I have three tables that represent a many to many relationship

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.