Rizky Ariestiyansyah Rizky Ariestiyansyah - 5 months ago 20
SQL Question

Auto fill other column in mysql table

I have column in mysql table like this:

| Column A | Column B |
| A | |
| B | |
| C | |
| D | |
| E | |


I want to auto fill the other column using rule if A,C,D then fill X and if B, E fill the Y in the column B field.

The result will be like this:

| Column A | Column B |
|----------|----------|
| A | X |
| B | Y |
| C | X |
| D | X |
| E | Y |


Is there are an easy way to do that in MySQL query?

Thank you for help.

Answer
UPDATE table
SET B = CASE
  WHEN A IN ('A','C','D') THEN 'X' 
  WHEN A IN ('B','E') THEN 'Y'
END