Solved

How to insert from a select where fields don't match in MySQL?

Posted on 2014-03-19
4
620 Views
Last Modified: 2014-03-19
Hi,

I want to create a table which is a copy of another tables data, but, this table is a log which will keep every change of every record that ever happens, as such I have added a new primary key and made the previous primary key just a regular integer value.

I want to select all data from one table and insert it in the other.
Both tables are identical except that the table I am inserting the data into has one extra field a primary key which I want to auto increment and does not exist in the table I am selecting from.

Naturally I get a MySQL error from the query below which would work fine if both tables where identical: #1136 - Column count doesn't match value count at row 1
INSERT `health4_mt_address_log` SELECT * FROM `health4_mt_address`;

Open in new window

0
Comment
Question by:jwleys
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
Comment Utility
Then enumerate the fields:
INSERT INTO `health4_mt_address_log` (field1, field2,.., fieldn)
   SELECT field1, field2, ..., fieldn FROM `health4_mt_address`

Open in new window

HTH,
Dan
0
 
LVL 27

Expert Comment

by:yodercm
Comment Utility
I asked a similar question some time back, and got a very clear excellent answer here:

http://www.experts-exchange.com/Database/MySQL/Q_28029891.html
0
 

Author Closing Comment

by:jwleys
Comment Utility
That works perfectly, looking at it, I don't know why I didn't think of it, dumb moment I guess :)

Thank you so much for your help!
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
Glad I could help!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
php survey script 4 57
Compare data between two databases 16 81
MySQL Trigger 8 28
Install MySQL 5.6 and PHP on Centos Linux 6 48
Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now