0

I have two MySQL tables with almost the same structure and I want to import the data from one of the tables into the other one. I run Codeigniter and I tried to select all the data from one of the tables and insert into the other but doesn't seem to work very good.

Table structure: url, text, source, img, date

  • I know it isn't a literal duplicate, but it's more than close enough to get you there. If you're still having problems after using INSERT SELECT, then please create a new question which includes the relevant code you're using, and what the error message is. – cwallenpoole Aug 18 '17 at 18:55
  • `INSERT INTO table1 (col1,col2,col3,col4,col5) SELECT column1,column2,column3,column4,column5 FROM table2` I hope this will work. – Rishi Malviya Aug 18 '17 at 18:56
  • @RishiKalal this is what I get when I ran that **Failed to read auto-increment value from storage engine**. The two tables have a small difference: one of them have a col named _id_ (with auto-increment) and the other doesn't. – Razvan Statescu Aug 18 '17 at 19:06

1 Answers1

1

In case you want to duplicate entries from table1 into table2 you can execute the following query:

Insert into table1 select * from table2

Or you can exclude the id column, as such:

INSERT INTO table1 (url,text,source,img,date) SELECT url, text, source, img, date FROM table2

In case you are using CodeIgniter 3, you can do it like this

 $select_result = $this->db->select('*')->get('table1');
            if ($select_result->num_rows()) {
                $this->db->insert_batch('table2', $select_result->result_array());
            }
Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164
AamirR
  • 11,672
  • 4
  • 59
  • 73