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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:11:00+00:00 2026-06-01T11:11:00+00:00

On my MySQL database, I have a unsigned mediumint column, which I keep incrementing

  • 0

On my MySQL database, I have a unsigned mediumint column, which I keep incrementing and decrementing its value.

However, I want to be sure that it doesn’t decrement when its value is 0. If I do, the UPDATE will make the value to take the maximum value of mediumint, 16777215.

So, I have this query when decrementing:

UPDATE `counters` SET `received`=IF(CAST(`received`-1 AS SIGNED)>0, `received`-1, 0) WHERE `id`="1234"

This was the only trick I got to work with only one query.

It works very well on MySQL 5.1, but, yesterday I upgraded to MySQL 5.5 and now I get these errors:

BIGINT UNSIGNED value is out of range in '(`db_main`.`counters`.`received` - 1)'

Is there any way to fix this?
Or… is there any other way to make sure, in only one query, that it won’t take the value of “16777215” when subtracting 1 to 0?

EDIT – I could use the WHERE, adding ” AND received > 0″. The thing is that sometimes I want to update two columns at the same time, and if I put all the columns > 0 in the WHERE clause, it won’t update ANY of the columns.

Thank you.

  • 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-01T11:11:01+00:00Added an answer on June 1, 2026 at 11:11 am

    That seems a little … torturous, with all that casting and conditional stuff (as an aside, per-row functions rarely scale well in big tables).

    Why don’t you just modify the where clause to stop the decrment if it’s already zero?

    update counters set received = received - 1
        where id = '1234' and received > 0
    

    If there are other columns you want updated irrespective of the value of received, you can do that with a transaction:

    begin transaction
    update counters set received = received - 1 where id = '1234' and received > 0
    update counters set xyzzy = 'plugh' where id = '1234'
    commit transaction
    

    This will decrement received, but not below zero, and it will change xyzzy no matter what. And it still keeps away from the per-row stuff that brings down many queries.

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

Sidebar

Related Questions

I have mysql database and I want a software which can draw the database
In mysql database i have this column called: Name: Date Type: datetime I have
So I have a mySQL database with a 'users' table which includes a username,
I currently have a MySQL database which I was hoping to use to store
I have a table in my Mysql database, which is used for authentication. And
I have a database table which contains an unsigned integer field to store the
I am designing the database (MySQL) in which I have two tables Employees and
I have a table in my database (Mysql) which has 6 columns. I am
In my mysql database I have the following information in a page name field.
In a MySQL database I have two tables linked in a join. One table

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.