MySQL - Update Field to Another Field's Value - mysql

I am sturggling to get the following query working:
UPDATE user_stock SET user_stock1 = user_stock2
WHERE user_stock1 = NULL AND user_id = 'mike';
The query is accepted with no syntax errors, although it does not set user_stock1 to the value of user_stock2, any ideas?

In SQL, a column is never equal to NULL. You have to use IS NULL or IS NOT NULL:
UPDATE user_stock SET user_stock1 = user_stock2
WHERE user_stock1 IS NULL AND user_id = 'mike';

Related

Replace value in SQL Table

I like to replace a 'NULL' value in FG_NFG_Selektion column but only in those row where Plant='935S'
Tried:
UPDATE [Table] SET [FG_NFG_Selektion] = REPLACE([FG_NFG_Selektion], 'NULL', 'FG') WHERE [Plant] = '935S'
Message back: 10000 rows affected but still the same 'NULL' is there in the table.
Try with following query
UPDATE [Table] SET `FG_NFG_Selektion` = 'FG' WHERE `Plant` = '935S' AND `FG_NFG_Selektion` IS NULL;

Update Query is not working. It returns with null value

I have simple Update query like,
$ekleme=mysql_query("UPDATE books SET adi = '$adi' WHERE id = '$id'");
But in the end, it returns with empty "adi" cell.

SQLite Query SET variable on a single row

I have a table called "allarmi_ingressi" in SQLite, with a lot of rows in it.
I want to create a query that changes the variable on my column "visto" to 1, if "visto=0", and to 0, if "visto=1".
This is what i made:
UPDATE allarmi_ingressi SET visto = '1' WHERE visto = '0'
Of course this modify every row in the column "visto";
I want to know if it's possible to modify it "selecting" it by the primary key, in my case "id_allarme".
In a SELECT query, you would use the WHERE clause to find rows with a specific id_allarme value.
The same WHERE clause can be used with UPDATE:
UPDATE allarmi_ingressi
SET visto = 1 - visto
WHERE id_allarme = ?;
Use CASE Expression
Query
update allarmi_ingressi
set visto = (
case visto when '1' then '0'
when '0' then '1'
else visto end
)
where id_allarme = __; -- id here

NULL != NULL in mysql query

I am trying to do a query that sees if fields are equivalent. However, whenever the field is NULL it returns a false result. I even tried doing the same thing with the column itself:
SELECT * FROM `mturk_completion` WHERE (`mturk_completion`.`imdb_url` =
`mturk_completion`.`imdb_url` AND `mturk_completion`.`worker_id` = 'A3NF84Q37D7F35' )
And it only returns results where the column is not NULL. Why is this so, and how do I get around it?
Your title is absolutely correct for any SQL implementation (not just MySQL). NULL is not equal to anything (including another NULL).
You need to use explicit IS NULL check or COALESCE() function (or its RDBMS-dependent alternatives) to set some default value in case of NULL.
Your comparison of mturk_completion.imdb_url to itself is redundant and should always return True, except when mturk_completion.imdb_urlis Null, in which case it will return Null.
That's because the operator = returns either True, False when comparisons can be made or Null, when either of the two operators is Null
Try this to illustrate the situation.
SELECT 1 = NULL; -- returns NULL
SELECT 1 != NULL; -- also return NULL
SELECT ISNULL(1 = NULL); -- returns 1
SELECT ISNULL(1 != NULL); -- returns 1
If you rewrite your query like below, your problems with ignoring NULLs will go away:
SELECT * FROM `mturk_completion` WHERE worker_id = 'A3NF84Q37D7F35'
I think you can use
(table.Field = table2.Field OR COALESCE(table.Field, table2.Field) IS NULL)

Update text field only if new value is not empty or null

In my table supportContacts I have a field called comments. I am trying to run an sql query that updates/appends new comments when the field is not empty or null. This comments are essentially coming from a textbox area inside a form, that when the user submits if empty the query should not update the field in the DB. The query below is giving me an error. SQLFIDDLE
UPDATE supportContacts SET IF(comments = CONCAT(comments, "Append this comment") IS NOT NULL or comments = '', 'empty', comments) WHERE id=1;
Put the condition in the where clause:
UPDATE supportContacts
SET Comments = CONCAT(comments, 'Append this comment')
WHERE id = 1 and Comments is not null and Comments <> '';
EDIT:
You can test for the appended value being NULL or empty as well:
UPDATE supportContacts
SET Comments = CONCAT(comments, #AppendComments)
WHERE id = 1 and Comments is not null and Comments <> '' and
#AppendComments is not null and AppendComments <> '';
But I think this is what you want:
UPDATE supportContacts
SET Comments = CONCAT(coalesce(comments, ''), #AppendComments)
WHERE id = 1 and #AppendComments is not null ;
This will do nothing if the new comments are empty. It will append new comments even when the existing ones are empty.