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.
Related
I have the following query which I am trying to run in MySQL. Basically I am trying to work out how to update the corresponding field which isn't null. I know its going to be one of two possible fields but I want to update the one which isn't empty. Maybe I am missing something blindingly obvious, but this is what I've tried thus far:
UPDATE `table`
#SET IF(FieldA IS NULL,FieldB,FieldA) = 1234
#SET IFNULL(FieldA,FieldB) = 1234
WHERE `FieldC` = '5678'
AND (
`FieldA` = '1234'
OR `FieldB` = '1234'
)
I suspect there may be a CASE solution but I'd prefer a shorthand/simple version option if it exists.
(My)SQL has no syntax that allows you to dynamically change the column you want to update, e.g. something like update ... set {PickFieldBaseOnCondition:FieldA|FieldB} = 1234. You have to specify a column there. The moment you start your query, the basic structure of the query (and all fields involved) have to be clear and fixed, only the values can change.
So you need to update both fields in your query if it shall be able to modify two different columns. But you can of course decide to just not modify the content of a field based on its content, e.g. keep it if it is null already:
update `table`
set FieldA = IF(FieldA IS NOT null, 1234, FieldA),
FieldB = IF(FieldB IS NOT null, 1234, FieldB)
where ...
Note that the requirement "update the other field if a field is null" only works if your initial condition that one field is null and one is not null is fulfilled, which you said is given. Otherwise, you should include a test if both fields are null or both fields are not null (in the comparison for the first field), which you could e.g. do with
update `table`
set FieldA = IF(FieldB IS null, 1234, null),
FieldB = IF(FieldB IS NOT null, 1234, FieldB)
where ...
FieldA can now be changed from content to null if both fields are not null (and from null to content if both fields are null), to enforce the condition that exactly one field is not null.
Please also note that IF() is a MySQL-only shorthand for CASE and doesn't work in all databases. You prefered a non-case solution, but it can trivially be rewritten using the sql-standard CASE.
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;
Just notice something weird while was saving 1 as integer into a ENUM('1', '0'). The value got stored as 0.
UPDATE table SET `somefield` = 1 WHERE `id` = 1;
SELECT id, `somefield` WHERE id = 1;
id, somefield
1, 0
Is there any way to make it work? I prefer to don't modify DB.
Also, any information about why this happen and the field is not converted would be very appreciated
When you provide an integer instead of a string for the update value of an enum, it's interpreted as the index of the enum values, not the value.
Enums, like all of SQL, use 1-based indexing, so for your enum index 1 is '1' and index 2 is '0'. Anything else is an error.
That means your update statement should result in a '1', not a zero, so either your table is not defined as you say, or your update statement is not as you say.
See this SQLFiddle proving this.
UPDATE table SET `somefield` = '1' WHERE `id` = 1;
The value needs to be specified as a string.
I have a table that has a foreign key to itself to keep track of ParentID. This is used to make a treelike hierarchy in the database. I'm trying to create a function that will find all rows that have a particular ParentID. However, if a record is a Root record it's parent is NULL. Therefore if I want to find all root tables I need to be able to enter NULL as an option as well. What I have so far is:
SELECT * FROM X.Y
WHERE
CASE
WHEN #ParentID IS NULL THEN ParentID IS NULL
WHEN #ParentID IS NOT NULL THEN ParentID = #ParentID
END
I think I'm on the right track, but haven't quite figured it out. It is saying Incorrect syntax near the keyword 'IS' indicating the IS in THEN ParentID IS NULL.
Any help would be great.
You're close. Try this:
SELECT * FROM X.Y
WHERE (#ParentID IS NULL AND ParentID IS NULL )
OR (ParentID = #ParentID )
About the first clause in my WHERE statement: any comparison to a NULL or a field whose value is NULL will always return false. Even WHERE NULL = NULL will evaluate as false.
So if you want to say, "Do this if both my variable and the field itself are null" you need to use something like WHERE (#ParentID IS NULL AND ParentID IS NULL ).
Some people like to do something like this:
WHERE (ISNULL(#ParentID,0) = ISNULL(ParentID,0)
It's tidy, but it doesn't allow the query engine to take advantages of the indexes that you might have on your table. It's considered a bad idea.
Try replacing the second WHEN to an ELSE or simply using an or like this
SELECT * FROM X.Y
WHERE ParentID IS NULL OR ParentID = #ParentID
In sql server, I see a strange behaviour:
When I do
select col1+ ' ' + cast(col2 as varchar(10)) as concat_col
it returns me a column with a value = NULL (not a blank column).
My suspicion is that it is because col2 has a value = NULL (not blank column).
So what is the reason for this behavior? But more importantly, what is the meaning of a column with value = NULL as opposed to a blank column? I do not imagine somebody went in the table and updated all the columns with value = NULL.
NULL means that the field does not have any value. You can use ISNULL function to convert a null value into something that you want.The following will hopefully give you the correct result (will not give null)
select ISNULL(col1,'')+ ' ' + cast(ISNULL(col2,'') as varchar(10)) as concat_col
Blank is a value while null is completely nothing.
Think of zero is a number while null is nothing.