简体   繁体   English

由于存在多个步骤,PSQL查询语法错误

[英]PSQL query syntax error because of multistep WHERE

I'm running a simple update query that is hitting all sorts of syntax errors and I can't figure out why. 我正在运行一个简单的更新查询,遇到各种语法错误,我不知道为什么。

This select runs fine: 此选择运行良好:

SELECT quote, author
FROM inspiration
WHERE length(quote)<250
AND used IS NULL
OR used > now()::date - 365
OFFSET floor(random()*10)
LIMIT 1;

While this get's a syntax error: 虽然这是语法错误:

UPDATE inspiration SET used=now()
WHERE length(quote)<250
AND used IS NULL
OR used > now()::date - 365
OFFSET floor(random()*10)
LIMIT 1;

Removing OFFSET floor(random()*10) LIMIT 1 solves the issue. 删除OFFSET floor(random()*10) LIMIT 1可解决此问题。 But I need one single random row updated. 但是我需要更新一个随机行。

Running EXPLAIN doesn't help since it's a syntax error. 运行EXPLAIN并没有帮助,因为它是语法错误。 Wrapping both WHERE statements in parenthesis doesn't help. 将两个WHERE语句包装在括号中无济于事。

But I need one single random row updated. 但是我需要更新一个随机行。

If I understand you correctly you want something like: 如果我正确理解您的要求,您将需要以下内容:

UPDATE inspiration
SET used = now()
WHERE id = (SELECT id          -- PK of inspiration table
            FROM inspiration
            WHERE length(quote)<250
              AND used IS NULL
                  OR used > now()::date - 365
            ORDER BY random() LIMIT 1);  

Also I belive that: 我也相信:

length(quote)<250
AND used IS NULL
OR used > now()::date - 365

-- should be
length(quote)<250
AND (used IS NULL
     OR used > now()::date - 365)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM