本文介绍了SQLite查询中的CASE语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个查询不起作用? :(我试图替换嵌套的IF语句"... SET lkey = IF(lkey> = 11,lkey-5,IF(lkey> 5,lkey + 2,lkey))"

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;

推荐答案

此子句(和类似的子句)中的语法错误

The syntax is wrong in this clause (and similar ones)

    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

因此,在您的情况下,它将显示为:

So in your case it would read:

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

查看文档(CASE表达式):

Check out the documentation (The CASE expression):

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

这篇关于SQLite查询中的CASE语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 20:02