这是有效的查询
UPDATE 18_6_TransactionPartners SET CompanyName = ?, VatCode = ?
WHERE CompanyName = ? OR RegistrationNumber = ?
我只想在一个查询中进行多次更新,例如
UPDATE 18_6_TransactionPartners SET CompanyName = ?, VatCode = ?
WHERE CompanyName = ? OR RegistrationNumber = ?
UPDATE 18_6_TransactionPartners SET CompanyName = ?, VatCode = ?
WHERE CompanyName = ? OR RegistrationNumber = ?
基于此http://blog.bubble.ro/how-to-make-multiple-updates-using-a-single-query-in-mysql/示例
UPDATE mytable
SET (title='Great Expectations' WHERE id='1'),
(title='War and Peace' WHERE id='2');
将查询更改为此
UPDATE 18_6_TransactionPartners
SET (CompanyName = ?, VatCode = ? WHERE CompanyName = ? OR RegistrationNumber = ?),
(CompanyName = ?, VatCode = ? WHERE CompanyName = ? OR RegistrationNumber = ?)
但得到错误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'
据了解,错误在SET
.
删除了括号。首先CompanyName = ?, VatCode = ? WHERE CompanyName = ? OR RegistrationNumber = ?
没有错误,但错误接近第二CompanyName = ?, VatCode = ?
什么是正确的语法?
也试过这个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
但得到错误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 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
所以对于每个UPDATE
只有一个SET RegistrationNumber = CASE
但可以设置多个条件WHEN CompanyName = 1 OR VatCode = 1
。
接下来要么尝试组合多个UPDATE 18_6_TransactionPartners SET RegistrationNumber = CASE
UPDATE 18_6_TransactionPartners SET RegistrationNumberNEXT = CASE
或需要创建多个查询....似乎需要尝试这个例子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