3

这是有效的查询

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
4

2 回答 2

4

不,这是不正确的,不会起作用。正如手册所说,UPDATE具有以下语法:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

请求中只能有 1 个 WHERE 子句。实际上,您的博客页面清楚地表明:“您不能像这样在单个 UPDATE 查询中对表进行多项更改”。

他正在使用 CASE 命令:

UPDATE mytable SET title = CASE
    WHEN id = 1 THEN 'Great Expectations'
    WHEN id = 2 THEN 'War and Peace'
ELSE title
END;

但 CASE 仅适用于 1 个变量:

CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

它不适用于像您这样的复合子句:

CompanyName = ? OR RegistrationNumber = ?

因此,您必须发送多个 UPDATE 请求。 编辑CASE从条件中删除了分号

于 2013-08-11T08:10:53.280 回答
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 回答