简体   繁体   中英

Copy values of one column to a new table

I'm trying to copy only a single column from a table to a newly created table.

Here is my SQL:

CREATE TABLE whatever(
            id INT NOT NULL AUTO_INCREMENT,
            PRIMARY KEY(id),
            uid INT,
            can_view TINYINT DEFAULT 0
        )

This works fine, however, I need to modify it so it populates the 'uid' column with the 'id' values from my users table.

Anyone have any idea how to go about this?

I tried

uid INT (SELECT id FROM users)

Which doesn't seem to work

INSERT INTO whatever (uid)
SELECT id
FROM users

INSERT ... SELECT

Or as you might like it:

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

CREATE TABLE ... SELECT

创建后-像这样:

INSERT into whatever( uid ) select id from users

Create as Select:

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;
insert into whatever(uid ) values select id from users

Here is the simple answer and it will surely help you.

INSERT into whatever(uid) VALUES (SELECT id FROM users)

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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