我正在尝试将表中的单个列复制到新创建的表中。
这是我的SQL:
CREATE TABLE whatever(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
uid INT,
can_view TINYINT DEFAULT 0
)
这很好用,但是,我需要对其进行修改,以便使用我的users表中的'id'值填充'uid'列。
任何人都知道如何解决这个问题?
我试过
uid INT (SELECT id FROM users)
哪个似乎不起作用
答案 0 :(得分:4)
INSERT INTO whatever (uid)
SELECT id
FROM users
或者你可能喜欢它:
CREATE TABLE whatever (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
uid INT,
can_view TINYINT DEFAULT 0
)
SELECT NULL AS id, id AS uid, 0 AS can_view
FROM users
答案 1 :(得分:1)
创建后 - 如下所示:
INSERT into whatever( uid ) select id from users
答案 2 :(得分:1)
创建为选择:
CREATE TABLE whatever(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
uid INT,
can_view TINYINT DEFAULT 0
) AS SELECT id AS uid FROM users;
答案 3 :(得分:0)
insert into whatever(uid ) values select id from users
答案 4 :(得分:0)
这是一个简单的答案,它肯定会对你有帮助。
INSERT into whatever(uid) VALUES (SELECT id FROM users)
感谢。