SQLite查询中的CASE语句

为什么这个查询不起作用? :(我尝试replace嵌套的IF语句“… SET lkey = IF(lkey> = 11,lkey – 5,IF(lkey> 5,lkey + 2,lkey))”

UPDATE pages SET lkey = CASE lkey WHEN lkey >= 11 THEN lkey - 5 ELSE CASE lkey WHEN lkey > 5 THEN lkey + 2 ELSE lkey END END, rkey = CASE lkey WHEN lkey >= 11 THEN rkey - 5 ELSE CASE rkey WHEN rkey < 11 THEN rkey + 2 ELSE rkey END END WHERE rkey > 5 AND lkey < 12; 

这个语句的语法是错误的(和类似的)

  CASE lkey WHEN lkey > 5 THEN lkey + 2 ELSE lkey END 

也是

  CASE WHEN [condition] THEN [expression] ELSE [expression] END 

要么

  CASE [expression] WHEN [value] THEN [expression] ELSE [expression] END 

所以你的情况是这样的:

  CASE WHEN lkey > 5 THEN lkey + 2 ELSE lkey END 

查看文档(CASEexpression式):

http://www.sqlite.org/lang_expr.html

另外,您不必使用嵌套的CASE。 你可以使用几个WHEN-THEN行,ELSE行也是可选的,尽pipe我推荐它

 CASE WHEN [condition.1] THEN [expression.1] WHEN [condition.2] THEN [expression.2] ... WHEN [condition.n] THEN [expression.n] ELSE [expression] END