Inserting Data Conditionally in MYSQL

Asked

Viewed 27 times

0

I have a table: conluna1, column2, column3.. The first two columns have integer data, 0 and 1, while the third column is without even a record. I would like to fill this column according to one condition, that is, if column 1 and column 2 are filled with the number 0, I would like to fill column 3 with the number 0, otherwise it would be number 1. Thank you.

1 answer

1


Use the clause CASE to make the decision:

UPDATE tabela
   SET coluna3 = CASE
                   WHEN coluna1 = 0 AND coluna2 = 0 THEN 0
                   ELSE 1
                 END

CASE

The CASE statement for stored Programs Implements a Complex conditional Construct.

In free translation:

The clause CASE for stored programs implements a complex conditional structure.

  • Gave buffer problem

  • Solved. Thanks for the code, helped bastaten.

  • Sorry, I can’t find that V, can you tell me where it is?

  • 1

    Okay, I’ve seen and I’ve clicked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.