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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:39:45+00:00 2026-05-16T16:39:45+00:00

I have two tables: +——- IPs ——+ +—- Groups —+ | Id (Int) |

  • 0

I have two tables:

+------- IPs ------+  +---- Groups ---+
| Id (Int)         |  | Id (Int)      |
| IP (String)      |  | Name (String) |
| Comment (String) |  | IPs (String)  |
+------------------+  +---------------+

The IPs-column of Groups is a comma-separated list of the IPS that this group consists of (The Id of the IP in the IPs-table of course).

I now want to find all IPs that have not yet been grouped -> No reference in any Groups IPs-field.

My idea was to somehow join all the Groups.IPs together and then search for all the IPs.IPs that are not in that list.

I’d really love to get that done completely in SQL (Programmatically is not the problem).

  • 1 1 Answer
  • 2 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-16T16:39:46+00:00Added an answer on May 16, 2026 at 4:39 pm

    I’m sure there’s some hacky solution to your problem, but if you want to “get that done completely in SQL”, I suggest refactoring your tables to avoid the comma-separated list of Ids. For a list of problems that comma-separated fields attract, I suggest checking out this recent Stack Overflow post:

    • Is storing a comma separated list in a database column really that bad?

    I suggest a table schema that looks something like this:

    CREATE TABLE ips (
       id int,
       ip varchar(255),
       comment varchar(255),
       PRIMARY KEY (id)
    );
    
    CREATE TABLE groups (
       id int,
       name varchar(255),
       PRIMARY KEY (id)
    );
    
    CREATE TABLE ip_groups (
       group_id int,
       ip_id int,
       PRIMARY KEY (ip_id, group_id),
       FOREIGN KEY (group_id) REFERENCES ips(id),
       FOREIGN KEY (ip_id) REFERENCES groups(id)
    );
    

    Then your query could be as easy as this:

    SELECT * 
    FROM   ips
    WHERE  NOT EXISTS (SELECT id 
                       FROM ip_groups 
                       WHERE ip_groups.ip_id = ips.id);
    

    Example:

    INSERT INTO ips VALUES (1, '127.0.0.1', 'localhost');
    INSERT INTO ips VALUES (2, '10.0.0.1', 'router');
    INSERT INTO ips VALUES (3, '10.0.0.100', 'my macbook');
    
    INSERT INTO groups VALUES (1, '10.0.0 subnet');
    INSERT INTO groups VALUES (2, 'computers');
    
    INSERT INTO ip_groups VALUES (1, 2);
    INSERT INTO ip_groups VALUES (1, 3);
    INSERT INTO ip_groups VALUES (2, 3);
    

    Result:

    +----+-----------+-----------+
    | id | ip        | comment   |
    +----+-----------+-----------+
    |  1 | 127.0.0.1 | localhost |
    +----+-----------+-----------+
    1 row in set (0.00 sec)
    

    The 127.0.0.1 IP does not belog to any group.


    UPDATE:

    You can also solve the above by using the “null-self-join” method:

    SELECT     ips.id, ips.ip, ips.comment
    FROM       ips
    LEFT JOIN  ip_groups ON (ip_groups.ip_id = ips.id)
    WHERE      ip_groups.group_id IS NULL;
    

    This is normally faster than the previous solution, because it does not use a correlated subquery. The problem with correlated subqueries is that they are executed once for each row in the outer query, and therefore performance suffers when the outer query (SELECT * FROM ips in this case) returns many rows.

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

Sidebar

Related Questions

I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables: users and user_depts. Let's say (for this question) that users
I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay
I have two tables CREATE TABLE table1 (id int primary key auto_increment,....); CREATE TABLE
I have two tables, namely Price List (Table A) and Order Record (Table B)
I have two tables: Events (ID, Name, Time, Recorder) Video (ID, StartTime, EndTime, Recorder,
I have two tables that are all the same, except one has a timestamp
I have two tables: COUNTRY ------------- id name NEIGHBOUR ------------- id id_country1 id_country2 id_country1
I have two tables that I want to use for viewing my reports which

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.