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
Related
I have a temporary table that I use to insert into the master db.
The temp table is named "temp_table"
The master table is "master"
I currently use the following command to update "master"
SELECT COUNT(*) FROM master;
SHOW COLUMNS FROM master;
INSERT INTO master
SELECT * FROM temp_table
ON DUPLICATE KEY UPDATE email = VALUES(email), phone = VALUES(phone)
Now, I want to be able to append field (counter) from the "temp table" into "master." The field already exists in both tables and I just want to be able to update or append it.
"counter" field in master may be empty or it may contain a number value already.
In cases where the value exists, it should append separated by a comma. Format (88,89,90)
In cases where the it's empty, it should update (88)
Thank you in advance.
I think you want:
on duplicate key update
email = values(email),
phone = values(phone),
counter = case when counter is null
then values(counter)
else concat(counter, ',', values(counter))
end
You can also phrase this with coalesce(), although the expression might be a bit more complicated to understand:
on duplicate key update
email = values(email),
phone = values(phone),
counter = concat(
coalesce(concat(counter, ','), ''),
values(counter)
)
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;
I need to anonymize emails in DB, so I try to set a query, but I cannot find a way to generate unique random string for each.
What I have so far is :
update candidate set email = (select CONCAT(SUBSTRING(MD5(RAND()) FROM 1 FOR 15) , '#test.fr'));
But obviously the value is not unique, is that possible with a simple query?
I tried solution here : https://harrybailey.com/2015/08/mysql-roughly-random-string-generation-for-updating-rows/ but same result, I got a
Error Code: 1062. Duplicate entry '0417da5fb3d071b9bd10' for key
'email'
You can use UUID
UPDATE `candidate` SET email = CONCAT(MD5(UUID()),'#test.fr');
and if you want exactly 15 characters
UPDATE candidate SET email=CONCAT(SUBSTRING(MD5(UUID()),1,15) , '#test.fr');
I am trying to use an update query to update fields from one table to another for fields but only if the fields in the table that i am updating into is blank. If they contain information, I do not want to overwrite the existing data
e.g
Field: Name
Table: Table 1
Update to: [Table2.][Name]
Criteria:
I am unsure of what to put in the criteria. I tried, 'Is Null', Like "".
Here is an example:
UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Looking at the Query from within Access, you can switch to SQL view. You just need to put Is Null in the criteria column: UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Furthermore, you can just write Is Null on alternate lines and it will count as OR.
I have the following issue. I have a table called titles with the following structure:
id int(10),
name varchar(100),
At some point later we added a new column called modified_name. It is defined as the same as name except that it is lower case and has all of the spaces replaced with a -. We added this column and so we needed to now get the right modified name value into each record of that column. To do this we wrote a PHP script that handled that by loading in values from the database and processing them, but that is highly inefficient. Is it possible to write a single UPDATE query that would add the correct value to each record in the titles table. I can think of ways to do this with a stored procedure and a while loop there in, but I want to know if something more efficient is possible. It there any way to achieve something like the following:
UPDATE `titles`
SET
`modified_name` = LOWER(REPLACE(SELECT `name` FROM `titles` WHERE id = PRESENT_VALUE), ' ', '-');
The goal being to SET the modified_title column of every record in the titles table to a unique value that results from that record's name column as followed:
# Before modification update query
name = "Hello Goodbye"
modified_name = ""
# After modification update
name = "Hello Goodbye"
modified_name = "hello-goodbye"
Thank you for your help, any advice on how best to do this would be appreciated.
UPDATE `titles` SET `modified_name` = LOWER(REPLACE(`name`, ' ', '-'))