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

The Archive Base Latest Questions

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

I have a server table which has a column named ‘SN’ in mysql, when

  • 0

I have a “server” table which has a column named ‘SN’ in mysql, when do query to retrive servers with some sns from ‘sn1’ to ‘sn10000’, we can:

select * from server where sn in ('sn1','sn2','sn3',...'sn10000');

If there is only one sn in ‘sn1’-‘sn10000’ which not exists in database, then the query above will retrive 9999 rows of result.

The question is how can I easily get which one in ‘sn1’-‘sn10000’ is not exists in database except the additional work, such as handling the result with shell script etc.

I have an ugly sql like below can use:

select * from (select 'sn1' as sn 
        union select 'sn2' 
        union select 'sn3'
        ....
        union select 'sn10000') as SN 
where not exists (select id from server where server.sn=SN.sn);

Is Anyone has other better methods? Thanks.

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

    Your query is perfectly fine for the intended use, but on MySQL the NOT IN and LEFT JOIN/IS NULL are more effecient that NOT EXISTS. Here are your alternatives:

    NOT IN

    SELECT * 
      FROM (          SELECT 'sn1' as sn 
            UNION ALL SELECT 'sn2' 
            UNION ALL SELECT 'sn3'
            ....
            UNION ALL SELECT 'sn10000') as SN 
     WHERE sn.sn NOT IN (SELECT s.id FROM SERVER s)
    

    LEFT JOIN/IS NULL

       SELECT s.id
         FROM SERVER s
    LEFT JOIN (          SELECT 'sn1' as sn 
               UNION ALL SELECT 'sn2' 
               UNION ALL SELECT 'sn3'
               ....
               UNION ALL SELECT 'sn10000') as SN ON SN.sn = s.id
        WHERE sn.sn IS NULL
    

    You might notice I used UNION ALL, rather than UNION – UNION removes duplicates (which won’t happen in your example), making it slower so UNION ALL is a better choice.

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

Sidebar

Related Questions

Database: Sql Server I have table column named page_text which contains following value I
I have a SQL Server table which has 625 columns created with different datatypes
I have a table in SQL Server 2005 which has a date time field.
I have an SQL Server 2005 table that has a varchar(250) field which contains
I have a SQL table in which some columns, when viewed in SQL Server
I have daily scheduled task that queries a table which includes column named AttemptDate
I have a table in MySQL which name is clienttable and it has 11
I have a Country table which has CountryName column. I need a ready made
I have a table which contains my ads that can be searched in sql-server-2008.
I am using Microsoft SQL Server. I have a Table which had been updated

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.