"没有这样的专栏"在WITH UPDATE语句中

时间:2014-06-09 22:49:06

标签: sqlite syntax

我正在直接查看sqlite documentation for the UPDATE statement,我有一些SQL,我相信它符合指定的语法:

CREATE TEMPORARY TABLE x (y INTEGER); 
INSERT into x SELECT value FROM table WHERE id IN (6,7,12,15) ORDER by value ASC; 
CREATE TEMPORARY TABLE z (y INTEGER);
INSERT INTO z (y) VALUES (6),(7),(12),(15);
WITH NewValues(f,g) AS
    (SELECT a.y AS f,b.y AS g FROM 
        (SELECT rowid,y FROM x) AS a,
        (SELECT rowid,y FROM z) as b
     ON a.rowid==(1+b.rowid%4)
    )
UPDATE table
SET value = NewValues.g
WHERE NewValues.f = table.id;

但是当我尝试执行此语句时,我收到错误:

near "WITH": syntax error

WITH之前的代码很好;如果我只是运行WITH的子选择并删除UPDATE的剩余部分,它将返回一个我期望的表。语法图表非常清楚地表明我可以将WITH放在语句的开头。那么我做错了什么?

更新: 我尝试过一个简单的examples给出:

WITH RECURSIVE
    cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<1000000)
SELECT x FROM cnt;

我也从中得到语法错误!我的第一个想法可能是我没有足够新版本的sqlite或其他东西,但我没有看到任何提及它的最低版本。

更新2: 我下载了最新的sqlite,它是3.8.5,而我运行的是3.7.12。这修复了WITH语法错误,因此它确实需要一个相当新版本的Sqlite。但是我仍然得到:

no such column: q.g

我在查询的上下文中无法理解,因为我已经用两种方式定义了它。

1 个答案:

答案 0 :(得分:2)

要引用WITH CTE,您需要从中进行选择。

试试这个:

CREATE TEMPORARY TABLE x (y INTEGER); 
INSERT INTO X (y) VALUES (6),(7),(12),(15);

CREATE TEMPORARY TABLE z (y INTEGER);
INSERT INTO z (y) VALUES (6),(7),(12),(15);


WITH NewValues(f,g) AS
    (SELECT a.y AS f,b.y AS g FROM 
        (SELECT rowid,y FROM x) AS a,
        (SELECT rowid,y FROM z) as b
    ON a.rowid==(1+b.rowid%4)
    )
UPDATE task
SET value = (SELECT g FROM NewValues WHERE NewValues.f = task.id)
WHERE id IN (SELECT f FROM NewValues);