MySQL Update NULL Field with Field=Field+1 Strange Behavior - mysql

not sure if u face this strange behavior on MySQL table before: when the table field is default as NULL, I do a simple UPDATE statement run no problem. if I were to issue field=field+1 then the value is not update. Then what i did is to insert 0 into the field and run the same field=field+1 again then it works.
UPDATE table1 SET field=field+1 WHERE id=123;
is this expected behavior?
p/s: the field type is double

If you have a null field then for update use.
//structure
UPDATE TableName SET column = IFNULL(column, 0) + 1 WHERE ...
//original
UPDATE table1 SET field = IFNULL(field, 0) + 1 WHERE id=123;
More details: function_ifnull
Ref# mysql-update-increment-int-field-that-is-null

It's because
NULL+1 = NULL
So you have to "trick" it to 0.
UPDATE table1 SET field=COALESCE(field,0)+1 WHERE id=123;

Related

Know how to write a mysql update query with trim function in set clause?

I need to make a update query that can allow me to update in trimming leading '0' in a particular column. I know I can write a select query like this to select trimmed value below:
SELECT TRIM(LEADING '0' FROM SkuCode) FROM MyTable WHERE Id=1;
But how can implement this is in update query. Please help me.
if the column is a text or varchar type try
UPDATE MyTable set SkuCode = SUBSTRING(SkuCode,2) WHERE Id = 1
If this SELECT statement is returning the value you want to assign to the skucode column:
SELECT t.skucode AS current_skucode
, TRIM(LEADING '0' FROM t.skucode) AS proposed_skucode
FROM MyTable t
WHERE t.id=1 ;
Then you can convert that into an UPDATE statement, by replacing SELECT ... FROM with UPDATE, and adding a SET clause before the WHERE clause, to assign the expression that returns the proposed value for sku code to the sku code column, e.g.
UPDATE MyTable t
SET t.skucode = TRIM(LEADING '0' FROM t.skucode)
WHERE t.id=1 ;

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

Setting a default value to existing field with null value - mysql

I have column in the database with value as null and i wanted
all the existing record with that column to be filled with the default
value as 1.
Kindly help
update yourtable set yourcolumn = 1 where yourcolumn is null;

mysql update increment int field that is null

I have a very large table with two INT columns that are null on Default. This is a problem because since they are INT fields, it would help in many cases if they were originally set to 0.
So my questions are, is there a way I can UPDATE and INCREMENT(+1) these fields while they are like this (null on Default)? BTW.. I didn't have luck so far, it seems increment only works when the default=0
..or is my only option to Change the Default to none from null
UPDATE TableName SET column = IFNULL(column, 0) + 1 WHERE ...
More info on IFNULL. It returns the first argument if it is not NULL, the second otherwise.
Try setting the field as NOT NULL to get away with the problem so that default value of 0 is used instead of null. The other option is to set column as zero whenever it is null.
UPDATE TableName SET FieldName = '0' WHERE FieldName IS NULL
Other alternative would be to issue IFNULL to return 0 in case the column is null and then incrementing the column.
UPDATE TableName SET FieldName = IFNULL(FieldName,0)
The SQL standard would be to use COALESCE(); this has been available in MySQL since version 3.23 (which was released into production in 2001):
UPDATE mytable
SET mycolumn = COALESCE(mycolumn, 0) + 1
WHERE my_other_columns = ...
I can't see any reason to choose IFNULL() over COALESCE() here.
Hope this helps.

How to prepend a string to a column value in MySQL?

I need a SQL update statement for updating a particular field of all the rows with a string "test" to be added in the front of the existing value.
For example, if the existing value is "try" it should become "testtry".
You can use the CONCAT function to do that:
UPDATE tbl SET col=CONCAT('test',col);
If you want to get cleverer and only update columns which don't already have test prepended, try
UPDATE tbl SET col=CONCAT('test',col)
WHERE col NOT LIKE 'test%';
UPDATE tablename SET fieldname = CONCAT("test", fieldname) [WHERE ...]
Many string update functions in MySQL seems to be working like this:
If one argument is null, then concatenation or other functions return null too.
So, to update a field with null value, first set it to a non-null value, such as ''
For example:
update table set field='' where field is null;
update table set field=concat(field,' append');
That's a simple one
UPDATE YourTable SET YourColumn = CONCAT('prependedString', YourColumn);
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column1) where 1
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2) where 1
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2, 'newtring2') where 1
We can concat same column or also other column of the table.