1

This is my table :

CREATE TABLE my_table (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL
);

And this is an existing table user :

CREATE TABLE user (
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL
);

Now, I want to INSERT into my_table using values from table user.

Using this query :

INSERT INTO my_table (id, firstname, lastname) 
VALUES (NULL, (SELECT firstname, lastname FROM user) );

I get this error :

Operand should contain 1 column(s)

How to do to insert using select query and auto_increment values?

2 Answers 2

2
INSERT INTO my_table (id, firstname, lastname) 
SELECT NULL, firstname, lastname FROM user

or better

INSERT INTO my_table (firstname, lastname) 
SELECT firstname, lastname FROM user
Sign up to request clarification or add additional context in comments.

Comments

0

Try with

INSERT INTO my_table (firstname, lastname) 
SELECT firstname, lastname FROM user;

Autoincrement will be manage by first table, where you defined it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.