Home  >  Article  >  Backend Development  >  请教怎样批量更新表值

请教怎样批量更新表值

WBOY
WBOYOriginal
2016-06-13 13:08:20789browse

请问怎样批量更新表值
现在我有一个数据表,其中有一个字段是type。数据表中有N条信息,
我希望更改 type的值,条件是根据不同的classid来更改。
但是现在又多组条件对应的多组type值
比如当class=1时,type=rass
class=8时,type=bbcc


还有多组这样的情况,请问怎样一次性一一对应的更新完所有的type。

我自己没办法了,刚学,我能想到的就是update table set type=相应值 where class=相应值

------解决方案--------------------
CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END
CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END 


http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#operator_case

------解决方案--------------------
你的数据库不符合第三范式,建议分解:新建(class,type),将type从原表中去除。
------解决方案--------------------
SQL中where查询符合条件的,然后更新
------解决方案--------------------
如果你的class和type对应关系没有规律的话,就不要用if,else或case when then来写了,不然得写多长?

你单独建立一个表,把class和type的对应关系存进去,更新表的时候与这个表做下联查

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn