0

我有两张桌子auth_usertemp

这些是他们的架构:

CREATE TABLE "auth_user" (
    "id" integer NOT NULL PRIMARY KEY,
    "username" varchar(30) NOT NULL UNIQUE,
    "first_name" varchar(30) NOT NULL,
    "last_name" varchar(30) NOT NULL,
    "email" varchar(75) NOT NULL,
    "password" varchar(128) NOT NULL,
    "is_staff" bool NOT NULL,
    "is_active" bool NOT NULL,
    "is_superuser" bool NOT NULL,
    "last_login" datetime NOT NULL,
    "date_joined" datetime NOT NULL
);

CREATE TABLE "temp" ( "id" integer, "username" varchar(30));

如果用户名相同,我希望将表中的id字段auth_user更新为表中的id字段。temp我怎样才能做到这一点?

我试过这个 SQL:

Update auth_user set id=(select temp.id from temp where temp.username=auth_user.username);

但我得到这个错误:

Error: datatype mismatch
4

1 回答 1

0

I found this question Update table values from another table with the same user name

It is similar to my question. Looking at the answer in that page, I tried this query

update auth_user set id=(select temp.id from temp where temp.username=auth_user.username) where exists (select * from temp where temp.username=auth_user.username);

and it works great. Same as my query in the question but with just an extra where clause. I don't get the Error: datatype mismatch now (but I don't know the exact reason why).

于 2012-08-29T06:09:18.173 回答