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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:25:35+00:00 2026-05-29T10:25:35+00:00

I have a MySQL table that looks something like this: |—ID—|—COUNTER—| | 1 |

  • 0

I have a MySQL table that looks something like this:

|---ID---|---COUNTER---|
|   1    |      2      |
|   2    |      6      |
|   3    |      1      |
|   5    |      9      |
|   6    |      10     |

I’m looking for a SELECT statement that returns ID’s and their COUNTER. The table only have ID’s such as: 1,2,3,5,6. Is there a statement where you say: I want ID’s 1 to 10 even if they doesn’t exist in the table, and if the ID doesn’t exist, return the ID anyway with the COUNTER value 0. For example:

|---ID---|---COUNTER---|
|   1    |      2      |
|   2    |      6      |
|   3    |      1      |
|   4    |      0      |
|   5    |      9      |
|   6    |      10     |
|   7    |      0      |
|   8    |      0      |
|   9    |      0      |
|   10   |      0      |

Do I have to create a SELECT statement that contains NOT EXIST parameters?

Thanks in advance, Steve-O

  • 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-29T10:25:35+00:00Added an answer on May 29, 2026 at 10:25 am

    Without creating a temp table:

    select t.num as id, coalesce(yt.counter, 0)
    from your_table yt
    right join (
        select 1 as num union select 2 union select 3 union select 4 union select 5 union
        select 6 union select 7 union select 8 union select 9 union select 10
    ) t on yt.id = t.num
    order by t.num
    

    and bit more general:

    select t.num as id, coalesce(yt.counter, 0)
    from your_table yt
    right join (
        select t1.num + t2.num * 10 + t3.num * 100 as num
        from (
            select 1 as num union select 2 union select 3 union select 4 union select 5 union
            select 6 union select 7 union select 8 union select 9 union select 0
        ) t1
        cross join (
            select 1 as num union select 2 union select 3 union select 4 union select 5 union
            select 6 union select 7 union select 8 union select 9 union select 0
        ) t2
        cross join (
            select 1 as num union select 2 union select 3 union select 4 union select 5 union
            select 6 union select 7 union select 8 union select 9 union select 0
        ) t3
    ) t on yt.id = t.num
    where t.num between (select min(id) from your_table) and (select max(id) from your_table)
    

    You can define limit by yourself here I’ve used min and max of id value from your_table.

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

Sidebar

Related Questions

I have a mysql table that looks something like this: Row 1: 'visitor_input_id' =>
I have a simple MySQL query that looks something like this SELECT * FROM
Let's say I have a MySQL table that is something like this: software table:
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
I have one table that looks something like this: id parent_id name 1 0
I have a table in MySQL That looks like the following: date |storenum |views
I have a table in a MSSQL database that looks like this: Timestamp (datetime)
I have a table that looks like this: (so you can picture it visually)
I have a Query that looks something like this (I know its vulnerable to
I have an INSERT statement that looks like this: INSERT INTO officer (officer_number, name,

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.