Here is query that works
UPDATE 18_6_TransactionPartners SET CompanyName = ?, VatCode = ?
WHERE CompanyName = ? OR RegistrationNumber = ?
I just want in one query make multiple updates, like
UPDATE 18_6_TransactionPartners SET CompanyName = ?, VatCode = ?
WHERE CompanyName = ? OR RegistrationNumber = ?
UPDATE 18_6_TransactionPartners SET CompanyName = ?, VatCode = ?
WHERE CompanyName = ? OR RegistrationNumber = ?
Based on this http://blog.bubble.ro/how-to-make-multiple-updates-using-a-single-query-in-mysql/ example
UPDATE mytable
SET (title='Great Expectations' WHERE id='1'),
(title='War and Peace' WHERE id='2');
Changed query to this
UPDATE 18_6_TransactionPartners
SET (CompanyName = ?, VatCode = ? WHERE CompanyName = ? OR RegistrationNumber = ?),
(CompanyName = ?, VatCode = ? WHERE CompanyName = ? OR RegistrationNumber = ?)
But get error Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(CompanyName = 'first name', VatCode = '123' WHERE CompanyName = 'first name' OR'
As understand, error is after SET
.
Removed brackets. For first CompanyName = ?, VatCode = ? WHERE CompanyName = ? OR RegistrationNumber = ?
no error, but error is near second CompanyName = ?, VatCode = ?
What is correct syntax?
Tried also this https://stackoverflow.com/a/15344247/2465936
UPDATE 18_6_TransactionPartners
SET value = CASE
WHEN CompanyName = ? OR RegistrationNumber = ? THEN CompanyName = ?, VatCode = ? ,
WHEN CompanyName = ? OR RegistrationNumber = ? THEN CompanyName = ?, VatCode = ?
ELSE CompanyName, RegistrationNumber END
But get error SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' VatCode = '123' , WHEN CompanyName = 'second name' OR RegistrationNumber = 'ttt'
Update
Experimented and works such code:
UPDATE 18_6_TransactionPartners SET RegistrationNumber = (
CASE
WHEN CompanyName = 1 OR VatCode = 1 THEN 1
WHEN CompanyName = 2 OR VatCode = 2 THEN 2
WHEN CompanyName = 3 OR VatCode = 'three' THEN 'three'
ELSE RegistrationNumber
END)
WHERE CompanyName IN ('1', '2', '3') OR VatCode IN ( 1, 2, 'three' ) //aim is to search table only where `WHERE` is true
So for each UPDATE
only one SET RegistrationNumber = CASE
but can set multiple conditions WHEN CompanyName = 1 OR VatCode = 1
.
Next either to try to combine multiple UPDATE 18_6_TransactionPartners SET RegistrationNumber = CASE
UPDATE 18_6_TransactionPartners SET RegistrationNumberNEXT = CASE
or need to create multiple queries.... Seems need to try this example http://code.openark.org/blog/mysql/multi-condition-update-query
UPDATE
film
SET
rental_duration=IF(rating = 'G', rental_duration+1, rental_duration),
rental_rate=IF(length < 90, rental_rate-0.5, rental_rate)
WHERE
rating = 'G'
OR length < 90