用Case解决Mysql问题,然后再用

时间:2013-04-17 05:51:06

标签: mysql

我正在维护一个至少有100行的数据库。每10秒进行一次ajax调用以更新行。我觉得每10秒调用100次更新查询是不可取的。所以我偶然遇到了mysql案例,然后将100个查询合并到一个查询中并进行更新。但是我遇到了代码问题。这是我的代码有两行

update customer_details set (date,customer_name,school_name,mobile_number,customer_email,age,area,customer_requirement,extra_comments,draft_message) = 
 case `id`
when '275' then ('09-04-201','person1','','1234567890','','','','basketball','','message')
when '274' then  ( '','','','','','','area','xyz','','message')
end

,错误是

#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 '(date,customer_name,school_name,mobile_number,customer_email,age,area,customer_r' at line 1

我无法纠正错误。还有其他方法可以对100行进行单个查询调用吗?

2 个答案:

答案 0 :(得分:1)

您需要在set语句中设置每一列,语法为:

UPDATE tablename SET column1 = "value1", column2 = "value2" ....
WHERE IdDColumn = MyID

在您的情况下,您将有两种选择:

UPDATE tablename SET Column1 =  
CASE
  WHEN id = 275 THEN Value1
  WHEN id = 274 THEN Value2
  ELSE DefaultValue 
END, Column2 =  
CASE
  WHEN id = 275 THEN Value1
  WHEN id = 274 THEN Value2
  ELSE DefaultValue  
END,
etc...

答案 1 :(得分:1)

你有没有尝试过:

INSERT INTO customer_details (id, date,customer_name,school_name,mobile_number,customer_email,age,area,customer_requirement,extra_comments,draft_message) VALUES
(275, '09-04-201','person1','','1234567890','','','','basketball','','message'),
(274, '','','','','','','area','xyz','','message')
ON DUPLICATE KEY UPDATE 
date=VALUES(date),
customer_name=VALUES(customer_name),
school_name=VALUES(school_name),
mobile_number=VALUES(mobile_number),
customer_email=VALUES(customer_email),
age=VALUES(age),
area=VALUES(area),
customer_requirement=VALUES(customer_requirement),
extra_comments=VALUES(extra_comments),
draft_message=VALUES(draft_message);