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

  • Home
  • SEARCH
  • 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 308601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:39:55+00:00 2026-05-12T07:39:55+00:00

In SQL, I’ve got a table that maps token=>count; it’s a dict where the

  • 0

In SQL, I’ve got a table that maps token=>count; it’s a dict where the default value is 0. If I want to increment a token, I can say:

insert into my_table (token, count)
values (my_token, 1)
on duplicate key update count = count + 1;

and if a token doesn’t exist yet, it’s inserted with count=1. Nifty.

Is there a similarly easy way to do the opposite in SQL, i.e., “given a token, decrement its count, and if count becomes 0 then just remove the record entirely”? I can think of longer ways to do it, but nothing as concise as the above.

  • 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-12T07:39:56+00:00Added an answer on May 12, 2026 at 7:39 am

    My advice is to use transactions as follows (my SQL is a bit rusty but you should get the idea):

    -- begin transaction
    update my_table set count = count - 1 where id = '7'
    delete from my_table where id = '7' and count = 0
    commit
    

    This will ensure the atomicity of the decrement-and-delete operation.

    However, one other possibility you may want to consider – don’t remove it at the point where it reaches zero. Since you say the default value is zero anyway, just leave the row in there with a value of 0.

    Of course, your queries will need to change to adapt to that. If you have one that lists active tokens, it will change from:

    select token from my_table
    

    to:

    select token from my_table where count > 0
    

    The decrement SQL in that case must be careful not to push the token count down to -1, so it would become:

    update my_table set count = count - 1 where id = '7' and count > 0
    

    That simplifies your SQL at the decrement time. If you still want those rows with zero-count to disappear, you could have another process that runs periodically to sweep them all up:

    delete from my_table where count = 0
    

    That’s just some alternatives to consider – if you really want them gone at the exact time their count reaches zero, then use the transaction method above.

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

Sidebar

Ask A Question

Stats

  • Questions 179k
  • Answers 179k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The modifier you need is x print_r(preg_match('/aaa bbb/x', 'aaabbb')); May 12, 2026 at 3:54 pm
  • Editorial Team
    Editorial Team added an answer The $ signifies an inner class. In this case: public… May 12, 2026 at 3:54 pm
  • Editorial Team
    Editorial Team added an answer HTML5 has no limits on the length of attribute values.… May 12, 2026 at 3:54 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In SQL Server, how do I floor a DATETIME to the second/minute/hour/day/year? Let's say
In Sql Server 2005 when I have multiple parameters do I have the guarantee
In SQL server How can I transform 1 row with varchar columns into a
In Sql Server 2008, when I create a table without schema prefix: create table

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.