How to access an update value from within an update statement in SQL - mysql

I am trying to do something like that in a prepared statement:
UPDATE table SET active = ?, last_action = IF(VALUES(active) > 0, NOW(), last_action) WHERE id = ?
So I am trying to access the value I am setting with SET active = ? in the same query.
However VALUES(active) does not seem to represent the value of active = ?
Is there any way to make this work like it would when the values are part of an INSERT INTO [...] VALUES [...] ON DUPLICATE KEY UPDATE ... statement?
Possible solution: add another parameter like this: IF(? > 0, NOW(), last_action) and set it to the same value as the active = ? parameter.
Is there any better solution that does not require me to bind the same value to another parameter?

Related

Sql replace is setting other value to null

Hey i'm trying to do some sql request but got a problem.
Firstly i do a replace request who filled my database.
After i do an update request that update some values.
And i'm doing another replace which is used to avoid duplicates, however some fields that were updated just before going to null because of the delete that is done before the insert (replace case)
How can I proceed so that the replace avoids putting some attribute to null and keep the values from the update?
Here is the sequence of the 3 queries that I make
REPLACE INTO all.serviceworker_subscriptions
(userId, userAgent, getDisplayMedia)
VALUES ('"+userId+"','"+userAgent+"','"+getDisplayMedia+"')
UPDATE all.serviceworker_subscriptions
SET dateDerniereConnexion = '"+socketDisconnectDate+"'
WHERE userId = '"+userId+"'
REPLACE INTO all.serviceworker_subscriptions
(userId, userAgent, getDisplayMedia)
VALUES ('"+userId+"','"+userAgent+"','"+getDisplayMedia+"')
My field : dateDerniereConnexion disappear after each 2nd replace ...
Thanks

I am trying to add data to a column in an existing row using sql

I am trying to add data into a column called address for a certain user. This is the code I am using;
Insert into register(address) values("Cork") where userId=1;
Try this:
Update register set address='Cork' where userId=1
If something already exists, we use Update.
I suspect you need update statement rather than insert :
update register
set address = 'Cork'
where userId = 1;
If you want to append a value, use the CONCAT() function, as in:
update register set address = concat(address, 'Cork') where userId = 1;
If you want to set the value, then a simple UPDATE will do, as in:
update register set address = 'Cork' where userId = 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

Update any of a number of fields in an SQL statement

Is there any way to optionally update any of a number of fields in an SQL statement? I can only get so far as something like:
UPDATE Customer SET
ContactName = ? <=== what to do here if ? is null???
, CompanyName = ? <=== what to do here if ? is null???
, AddressId = ? <=== what to do here if ? is null???
, ...
WHERE CustomerId = ?
It must be a single statement due to a limitation of the wrapper layer and MySQL.
To clarify, I want to create one statement which will be run with one or more non-null values for the fields listed and will update only those fields where the value is not null, leaving the others alone.
This is for a RESTful update API where I don't want a separate statement for every possible combination of fields and I don't want to demand that the API caller supply every single field on the record, which would be unwieldy and would not be future-proof (adding a field breaks existing API calls). And because it's a REST API I am looking to map POST /customer/xxx?ContactName=...&CompanyName=... to update the record.
Not entirely clear on your goal, but I think you're looking for the coalesce feature; it will return the first non-null value:
mysql> SELECT COALESCE(NULL,1); -> 1
mysql> SELECT COALESCE(NULL,NULL,NULL); -> NULL
So you can set a 'default' to use in case the value is null:
UPDATE Customer SET
ContactName = COALESCE(?, <Some default>)
...
If you don't want to change the field, just use the current value:
UPDATE Customer SET
ContactName = COALESCE(?, ContactName)
Usually you would want to use the existing value if the parameter is null in an update:
UPDATE Customer SET
ContactName = COALESCE(?,ContactName)
UPDATE Customer SET
ContactName = ifnull(?,ContactName)
, CompanyName = ifnull(?,CompanyName)
, AddressId = ifnull(?,AddressId)
WHERE CustomerId = ?
SQL Fiddle

On/Off query in mysql?

I use an update query in mysql to change the published row in my db to 1 or 0. Is there a way to do this automatically in one query, ie, if the database had a 1 in published it would set it to 0 and if it had a 0 it would set it to 1... just like a switch?
Thanks
Assuming the field's a boolean/bit field, then
UPDATE table SET bitfield = ~bitfield
where ~ is mysql's bit-wise NOT operator.
If the field is INT, then:
UPDATE table
SET field = 1 - field
WHERE ...
UPDATE tbl SET published = !published WHERE ...
You can do this with a conditional query. Something like this for example
UPDATE table SET published = IF(published = 1, 0, 1)