3

これが機能するクエリです

UPDATE 18_6_TransactionPartners SET CompanyName = ?, VatCode = ? 
WHERE CompanyName = ? OR RegistrationNumber = ?

次のように、1つのクエリで複数の更新を行いたいだけです

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 = ?はエラーはありませんが、エラーは2番目に近いです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

そのため、それぞれにUPDATE1 つだけ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
4

2 に答える 2

2

ケースが必要です:

UPDATE mytable
SET title= (CASE WHEN id='1' THEN 'Great Expectations' ELSE 'War and Peace' END) 
WHERE id IN ('1','2');
于 2013-08-11T08:06:25.767 に答える