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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:58:53+00:00 2026-06-05T02:58:53+00:00

I need to group IP list by subnet (first 3 octets) to count it.

  • 0

I need to group IP list by subnet (first 3 octets) to count it. For example, if I have Ips

123.12.12.12
123.12.12.11
123.12.11.11

I have to get such result:

123.12.12 | 2
123.12.11 | 1

I googled this expample:

select 
substr(ip,1,locate('.',ip,locate('.',ip)+1)-1) 
as ip, count(ip) as count
from ip_list
group by ip ORDER BY count  DESC

But it groups the list only by first two octets. I was lost in all these locate(locate(locate(...))). Can somebody help to modify this to get proper results?

  • 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-05T02:58:54+00:00Added an answer on June 5, 2026 at 2:58 am

    You should have used group by expression name.

    select 
    --   locate( '.', ip, locate( '.', ip, locate( '.', ip ) + 1 ) + 1 ) as l,
       substr( ip, 1, locate( '.', ip
                              , locate( '.', ip
                                        , locate( '.', ip ) + 1 ) + 1 ) - 1 ) as subip,
       count(ip) as count
    from ip_list
    group by ( subip )
    order by count desc
    ;
    

    EDIT 1:
    Use of locate is not required. SUBSTR_INDEX can be used to filter the subset of IP’s.

    Example:

    select
           substring_index( ip, '.', 3 ) as subip
         , count(ip) as count
      from ip_list
     group by ( subip )
     order by count desc
    

    Refer to Documentation:

    • MySQL: SUBSTR(str,pos,len)
      • Return a substring len characters long from string str, starting at
        position pos.
    • MySQL: SUBSTRING_INDEX(str,delim,count)
      • Returns the substring from string str before count occurrences of the
        delimiter delim.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of specific objects(meetings) which I need to group using a
I have a list of customer and i need to sort and group the
I have a list of MyObjects which I need to divide into three groups:
I have got a group of radio buttons that when clicked need to display
I have a form that has a group of 3 text_fields. I need two
I don't need the laziness of itertools.groupby. I just want to group my list
I need code which creates boolean list depended on inserted list and count. For
I have the following data table and i need to group it by quarters.
Recently a question raised in my group. I have a sections list in database.it
I need to get a list of Articles sorted by the latest Comment from

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.