I have a table where a plugin keeps all form submissions, in this table I have a row for each field like this:
1 'Name' 'Tony'
2 'Surname' 'Smith'
3 'Mail' 'tony@mail.com'
4 etc.
I want to take all this data to a table where all data is in columns, like this:
New_Id Name Surname Mail
1 'Tony' 'Smith' 'tony@mail.com'
2 'Mary' 'DotCom' 'mary@dotcom'
I have a A LOT of these records so efficiency is important. My first approach would be a PHP loop on every field in first table and then decide if I have to insert or update in the second table, this solution might even need a previous select to see if field is already populated so the solution is not very nice.
Can you think of a better solution?
EDIT: This is my structure:
This is my target table, field names are in Spanish but they are name, birth_date, adress, etc.
create table target_table
(id int not null AUTO_INCREMENT,
blog_id int,
submission_id int,
form_id varchar(250) character set utf8 collate utf8_general_ci,
submission_date timestamp,
ip varchar(250) character set utf8 collate utf8_general_ci,
sitio varchar(250) character set utf8 collate utf8_general_ci,
nombre varchar(4000) character set utf8 collate utf8_general_ci,
fecha_nacimiento varchar(250),
email varchar(250) character set utf8 collate utf8_general_ci,
direccion varchar(250) character set utf8 collate utf8_general_ci,
cp varchar(250) character set utf8 collate utf8_general_ci,
PRIMARY KEY(id));
And source are actually two tables, cforms_submissions is parent with submission_id, submission_date and email:
id int(11) unsigned PK
form_id varchar(3)
sub_date timestamp
email varchar(40)
ip varchar(15)
And cforms_data is child table with fields and their values:
f_id int(11) unsigned PK
sub_id int(11) unsigned <---- This is FK to parent
field_name varchar(100)
field_val text
So, I should do something like:
select submissions....
for each submission
select fields in submissions
for each field in submission
if first_field
insert
else
update
Maybe is there a better approach?
Thank you, BTW, I’m working with MySql
Sorry, I had misunderstood the question.
As long as the number of tables if finite you can do N joins in the following way:
What you are doing is for each field in the target getting the subset of entries that match that field in the source, and for those matching it with the field in the other subsets with the same ID.
The query is going to be painfully long since you’ll have an alias per field, but the performance shouldn’t be terrible on if it is properly indexed (probably on field_name,sub_id). I think you can add the field_name comparison in the form, but I am not sure if that is MySQL or only Oracle.
You probably need the parent for the email, but I that is trivial
ORIGINAL:
Just execute in MySql
I am assuming you want to insert the last record. Otherwise, vary the SELECT accordingly. The fields in the INSERT INTO have to match the ones in the SELECT.