MySQL - if value in column then insert value in another column - mysql

I want to do bulk update on my database. So I want to know how to insert value to the new column based on the value from other column. E.g.
If value in columnA = 15, then insert 'text' in columnB.
Thanks.

Thats not insert, thats update,and you can do it as
update table_name
set
columnB =
case when columnA = 15 then 'text' end

I've found the answer from almost similar question.
UPDATE table
SET columnB='text' where (columnA='15')
This is much more straight forward.

UPDATE table SET columnB = IF(columnA = 15, 'text',NULL);

Related

How do I add some values to the SQL table without the insert into select statement ?

I want to add the values to the columns where name=Michael for example it seems there is no way to do that
I think you want to ask that you want to update values in columns of a table in which name = Michael.
For that :
update tablename
set col1='val1', col2='val2', col3='val3'
where name = 'Michael'

How do i update a record in mysql table row

I am stuck with following statement. i dont know where the problem is. can someone please look into it and let me know, is it correct or not and if not than whats the correct one to update the row.
SELECT id FROM records WHERE user_id = 12119 AND field_id = 9
UPDATE records (user_id, field_id, value) VALUES (12119, 9, 'dallas')
You probably want to update value column for user with id 12119.
UPDATE records
SET value ='dallas'
WHERE user_id = 12119
AND field_id = 9;
use mysql update query like this
UPDATE table_name
SET column1=value, column2=value2
WHERE some_column=some_value

Update the value of a field on UPDATE of another IF it equals a certain value

I have one field in a table and I'm updating it often; what I want to do is if when that field is updated it equals the same value of another field then update another field.
Let me explain, basically a quicker way of doing this:
UPDATE my_table SET spots_taken=spots_taken+1 WHERE id=1234;
UPDATE my_table SET open=1 WHERE id=1234 AND spots_taken=spots;
Can this be done in one query?
Try using CASE:
UPDATE my_table
SET spots_taken=spots_taken+1,
open = (CASE WHEN spots_taken=spots THEN 1 ELSE open END)
WHERE id=1234

Copy value of one column to another column, but only where fields have no value MySQL

I have looked for an answer to this and can't find what I need. I dare not try anything I have made up...
I want to copy the value of one column (column a) to another column to (column b) but I only want this to happen to fields within column b that have no current value.
Thanks in advance for any help.
Try
UPDATE table1
SET b = a
WHERE b IS NULL
Here is SQLFiddle demo
If b is of VARCHAR type and you consider empty strings also as having no value then you can do
UPDATE table1
SET b = a
WHERE CHAR_LENGTH(COALESCE(b, '')) = 0
Here is SQLFiddle demo
You can just UPDATE your table and set your columnb same as column a only where column b is null
UPDATE table
SET columnb = columna
WHERE columnb IS NULL
If your column have empty strings just use WHERE columnb = ''
UPDATE tablename
SET row1 = row2
WHERE row2 IS NULL OR row2 = ''
This will consider both NULL and empty rows.
Otherwise - just post what you made up, so we can verify it.
So like:
UPDATE MyTable
SET ColumnB = ColumnA
WHERE ColumnB IS NULL;

Insert the same fixed value into multiple rows

I've got a table with a column, lets call it table_column that is currently null for all rows of the table. I'd like to insert the value "test" into that column for all rows. Can someone give me the SQL for this?
I've tried INSERT INTO table (table_column) VALUES ("test"); but that only populates that last row. How do I do all of the rows at once?
You're looking for UPDATE not insert.
UPDATE mytable
SET table_column = 'test';
UPDATE will change the values of existing rows (and can include a WHERE to make it only affect specific rows), whereas INSERT is adding a new row (which makes it look like it changed only the last row, but in effect is adding a new row with that value).
This is because in relational database terminology, what you want to do is not called "inserting", but "UPDATING" - you are updating an existing row's field from one value (NULL in your case) to "test"
UPDATE your_table SET table_column = "test"
WHERE table_column = NULL
You don't need the second line if you want to update 100% of rows.
To update the content of existing rows use the UPDATE statement:
UPDATE table_name SET table_column = 'test';
What you're actually doing is adding rows. To update the content of existing rows use the UPDATE statement:
UPDATE table SET table_column = 'test';
UPDATE `table` SET table_column='test';
The SQL you need is:
Update table set table_column = "test";
The SQL you posted creates a new row rather than updating existing rows.
To create a new empty column and fill it with the same value (here 100) for every row (in Toad for Oracle):
ALTER TABLE my_table ADD new_column INT;
UPDATE my_table SET new_column = 100;