5

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

这是我尝试过的命令:

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=...

希望你能帮我

4

3 回答 3

4

Your query doesn't make any sense.

DELETE FROM database_userprofile WHERE user.username = 'some';
                                       ^^^^

Where'd user come from? It isn't referenced in the query. Is it a column of database_userprofile? If so, you can't write user.username (unless it's a composite type, in which case you would have to write (user).username to tell the parser that; but I doubt it's a composite type).

The immediate cause is that user is a reserved word. You can't use that name without quoting it:

DELETE FROM database_userprofile WHERE "user".username = 'some';

... however, this query still makes no sense, it'll just give a different error:

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...

My wild guess is that you're trying to do a delete over a join. I'm assuming that you have tables like:

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
);

and you're trying to do delete with a condition across the other table.

If so, you can't just write user.username. You must use:

DELETE FROM database_userprofile
USING "user"
WHERE database_userprofile.user_id = "user".id
AND "user".username = 'fred';

You'll notice that I've double-quoted "user". That's because it's a keyword and shouldn't really be used for table names or other user defined identifiers. Double-quoting it forces it to be intepreted as an identifier not a keyword.

于 2013-06-08T02:16:41.963 回答
0

由于文档的原因,PostgreSQL 9.1 中删除的语法是:

[ WITH [ RECURSIVE ] with_query [, ...] ]
DELETE FROM [ ONLY ] table [ * ] [ [ AS ] alias ]
    [ USING using_list ]
    [ WHERE condition | WHERE CURRENT OF cursor_name ]
    [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]

所以你需要在 DELETE 命令之后指定“table_name”,而不是“database_name”。
只有登录到数据库后才能删除数据。

你得到
ERROR: syntax error at or near "."
因为在 WHERE 部分中,您可以指定目标表或 usinglist 中的表。

于 2013-06-07T15:35:08.203 回答
-1

在将查询从 Eclipse 复制粘贴到 pgadmin 时,您也可能会收到此错误。不知何故,可能会插入一个奇怪的符号。为避免此错误,请先将其粘贴到简单的文本编辑器(如记事本)中,然后从那里剪切并粘贴到 pgadmin。

于 2015-11-04T08:24:11.510 回答