SQLite 查询中的 CASE 语句
为什么这个查询不起作用? :( 我尝试替换嵌套 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;
Why this query doesn't work? :( I tried to replace nested IF statement "...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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该子句(以及类似的子句)中的语法是错误的,
它是
或者
因此在您的情况下它将显示为:
查看文档(CASE表达式):
http://www.sqlite.org/lang_expr.html
The syntax is wrong in this clause (and similar ones)
It's either
or
So in your case it would read:
Check out the documentation (The CASE expression):
http://www.sqlite.org/lang_expr.html
此外,您不必使用嵌套 CASE。您可以使用多个 WHEN-THEN 行,并且 ELSE 行也是可选的,尽管我推荐它
Also, you do not have to use nested CASEs. You can use several WHEN-THEN lines and the ELSE line is also optional eventhough I recomend it