Put value on everything null in mysql

Asked

Viewed 4,944 times

0

I have the following problem,I created a table in mysql added all the records and after that I created a foreign key in it, this table contains information of only one "thing", however in the future it will have more things. I wanted to know if there is a query that replaces everything that is Null by another value,it is no use to change the default because in the future this value will have to change

  • 3

    update tabelea set campo = 'outro valor' where campo is null. Not?

  • '-' oh God........

  • 1

    @Rafael, it was I who gave the -1 and I explain why: It does not present research effort. If you have something to argue, you are at ease

  • This is not the place nor the time. However, help more. Don’t just play -1. And if you are so sure of this, why did you withdraw?

2 answers

5

From what I understand you had something like this:

TabelaX
|--------------------|
| ColA | ColB | ColC |
|------|------|------|
| 1    | 'ab' |  3   |
| 2    | 'ac' |  3   |
| 3    | 'ad' |  5   |
|--------------------|

You have created a new column and the existing values are with NULL;

TabelaX
|---------------------------|
| ColA | ColB | ColC | ColD |
|------|------|------|------|
| 1    | 'ab' |  3   | null |
| 2    | 'ac' |  3   | null |
| 3    | 'ad' |  5   | null |
|---------------------------|

You want to change to a specific value, just use:

UPDATE TabelaX SET ColD = 1;

If you can’t, it may be that the database server has secure update mode enabled (i.e., you need a clause where to the update work), you could use 1=1 that will always be true and will update all records.

UPDATE TabelaX SET ColD = 1 WHERE 1=1;

Upshot:

TabelaX
|---------------------------|
| ColA | ColB | ColC | ColD |
|------|------|------|------|
| 1    | 'ab' |  3   |  1   |
| 2    | 'ac' |  3   |  1   |
| 3    | 'ad' |  5   |  1   |
|---------------------------|
  • 1

    It was that same friend, I had tried so but was with safe update mode, the second way worked. Obg

3

The command to update records is UPDATE, which has the following syntax:

UPDATE nome_tabela
SET CAMPO = "novo_valor"
WHERE campo is null

Do this when you need to update the fields that are null, which is your case.

Browser other questions tagged

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