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

The Archive Base Latest Questions

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

I want to update a table in MySQL like this: UPDATE Table SET A

  • 0

I want to update a table in MySQL like this:

UPDATE Table
SET A = '20' IF A > 20
SET A = A    IF A < 20 
SET A = 0    IF A <= 1
WHERE A IS NOT NULL;

But the above SQL is not valid Syntax. I also tried this:

UPDATE table
SET A = IF(A > 20, 20, IF(A < 20, A, 0));

But is also invalid Syntax. How do I use an if statement in an update query like this?

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

    I think you were 99% there:

    UPDATE table
    SET A = IF(A > 20, 20, IF(A < 20 && A > 1, A, 0))
    WHERE A IS NOT NULL;
    

    Add the && A > 1 to the second IF statement and your third condition is satisfied.

    Edit:

    Per @Andre’s comment to the question and the suggestion that the nested IF is difficult to read, you could also do this as a couple of queries that don’t do any unnecessary work and are readable:

    UPDATE table SET A = 20 WHERE A > 20;
    UPDATE table SET A = 0 WHERE A <= 1;
    

    When A is NULL, it will not meet either of these conditions, and thus eliminates the need to specify that A not be NULL.

    Next, there’s no need for the third condition as @Andre suggested. If A is between 1 and 20, it gets left as-is.

    Finally, setting A to 0 where A is less than or equal to 1 seems unusual. Values of 1 will be changed to 0. If you intend to simply set values less than 1 (including negative values) to 0, then you should swap < for <=.

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

Sidebar

Related Questions

hi want to run a query like this: UPDATE `stories` SET `position`=(select @i:=@i+1) WHERE
I have a table defined like this: CREATE TABLE mytable (id INT NOT NULL
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
I want to insert / update data into a MySQL table by using a
I want to update a table to indicate that some rows are parents of
I want to update two columns in a table. The value of the second
I want to update a column in a table making a join on other
I want to Update Column of a Table where ColumnName will be DropDown.SelectedValue. Example:
I have a Table View Controller with cells. I want to update the text
Often I want to add a value to a table or update the value

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.