Fakipo
Fakipo

Reputation: 190

How to insert into one table null value and select * from another table in mysql?

I have one extra (id AI) column in B table, How can I fill all the data from A table and set id as null?

Table A
|---------------------|------------------|
|          id         |  EmployeeName    |
|---------------------|------------------|
|           1         |       ABC1       |
|---------------------|------------------|
|           2         |       ABC2       |
|---------------------|------------------|
|           3         |       ABC3       |
|---------------------|------------------|

Table B
|----------------------|---------------------|------------------|
|          id          |       empid         |  EmployeeName    |    
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|

Here we only have 2 columns but say we have many columns corresponding to a dynamic query.

here is the query I am trying

insert into B (select null, * from A);

Upvotes: 0

Views: 1866

Answers (2)

FanoFN
FanoFN

Reputation: 7114

This way should do it:

INSERT INTO B (SELECT NULL, A.* FROM A);

You just need to append the table name A. to the Asterix.

Upvotes: 1

BlackSwan
BlackSwan

Reputation: 384

You can try inserting only to the specified columns in B table. Then the column which is not present automatically gets populated as NULL

INSERT INTO table2 (empid,EmployeeName)
SELECT Id,EmployeeName
FROM table1
WHERE condition;

Upvotes: 3

Related Questions