DELETE FROM ... 报告“.”处或附近的语法错误

Sas*_*ash 6 database postgresql row delete-row sql-delete

我试图从数据库中仅删除一项数据,但是,当我编写命令时,我不断收到一些语法错误,您能告诉我错误在哪里吗?

这是我尝试过的命令:

DELETE FROM database_userprofile WHERE user.username = 'some';
ERROR:  syntax error at or near "."
LINE 1: DELETE FROM database_userprofile WHERE user.username = 'some'...

DELETE FROM database_userprofile USING database_user WHERE user.username="some";
ERROR:  syntax error at or near "."
LINE 1: ... database_userprofile USING database_user WHERE user.username=...
Run Code Online (Sandbox Code Playgroud)

希望你能帮我

Cra*_*ger 5

你的询问没有任何意义。

DELETE FROM database_userprofile WHERE user.username = 'some';
                                       ^^^^
Run Code Online (Sandbox Code Playgroud)

从哪里来user的?查询中未引用它。它是一列吗database_userprofile?如果是这样,你就不能写user.username(除非它是复合类型,在这种情况下你必须写(user).username来告诉解析器;但我怀疑它是复合类型)。

直接原因是这user是一个保留字。您不能在不引用该名称的情况下使用它:

DELETE FROM database_userprofile WHERE "user".username = 'some';
Run Code Online (Sandbox Code Playgroud)

...但是,这个查询仍然没有意义,它只会给出不同的错误:

regress=> DELETE FROM database_userprofile WHERE "user".username = 'some';
ERROR:  missing FROM-clause entry for table "user"
LINE 1: DELETE FROM database_userprofile WHERE "user".username = 'so...
Run Code Online (Sandbox Code Playgroud)

我的疯狂猜测是您正在尝试通过连接进行删除。我假设你有这样的表:

CREATE TABLE "user" (
    id serial primary key,
    username text not null,
    -- blah blah
);

CREATE TABLE database_userprofile (
     user_id integer references "user"(id),
     -- blah blah
);
Run Code Online (Sandbox Code Playgroud)

并且您正在尝试使用另一个表中的条件进行删除。

如果是这样,你就不能只写user.username。您必须使用:

DELETE FROM database_userprofile
USING "user"
WHERE database_userprofile.user_id = "user".id
AND "user".username = 'fred';
Run Code Online (Sandbox Code Playgroud)

您会注意到我用双引号“用户”。这是因为它是一个关键字,不应该真正用于表名称或其他用户定义的标识符。双引号强制将其解释为标识符而不是关键字。