1

i have to create a new column in my SQL db. The new column should have some data from two other existing colums.

I thought this is a simply SQL command, but it doesn't fill the new column with data

ALTER TABLE table1 ADD COLUMN new_C text;
INSERT INTO table1 (new_C)
SELECT IFNULL (title_1, title_2) from table1;

The new column is created, but it is filled with "NULL". Can somebody help me, what i'm doing wrong?

Thank you

Bodaggnmo
  • 17
  • 5
  • INSERT INTO `table1` (`new_c`) VALUES ([value-1]); this should be your query – Parth Shah Aug 20 '18 at 10:41
  • I thought VALUES add single cells to the column, but in my case, there are 1200 lines at the old column title_1. Some cells are filled with NULL, and that's why i used the command "SELECT IFNULL" to get the data of the whole column and if there is a NULL to get the data from title_2 instead. With Values i have to do this command for every single cell? – Bodaggnmo Aug 20 '18 at 10:45
  • yes you need to map which column you want for null column. – Parth Shah Aug 20 '18 at 10:56
  • try this https://stackoverflow.com/questions/3215454/mysql-ifnull-else – Parth Shah Aug 20 '18 at 10:57
  • Thanks for your help. But unfortunatelly the result is the same. In the new column new_C is NULL in every cell.. – Bodaggnmo Aug 20 '18 at 11:04

1 Answers1

1

What you actually wanted was an UPDATE command:

UPDATE table1 SET newC = IFNULL (title_1, title_2);

As a result of your INSERT command you will probably have a lot of rows with only values of newC in them which you will need to delete.

Nick
  • 138,499
  • 22
  • 57
  • 95