TomJ TomJ - 2 months ago 4
MySQL Question

MySQL: Move data from 61 column table to 58 column table

I'm a pretty green novice with SQL still and have been tasked with something that looks incredibly tedious to accomplish. I need to move 58 of 61 columns from tableA to tableB. tableA has 61 columns and only 15 rows, tableB has 58 columns and zero rows right now. I've read multiple sources on how to achieve this (moving data from one table to a related one), but none specifically talk about this particular case I'm dealing with.

To further complicate things, the second table does not have the columns in the same order as the first, so this poses a problem as well. This SO post seemed promising, but it is working with a substantially smaller dataset: SQL: Move column data to other table in same relation . This one is also similar MYSQL - Move data from one table to a related one? . The problem is they both are moving to a larger table from a smaller table, not vis a vis.

Here's my initial query for attempting to make this transition happen (and was unsuccessful as the included error indicates):

INSERT INTO tableB SELECT * FROM tableA;
ERROR 1136 (21S01): Column count doesn't match value count at row 1

tableA
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| staff_fk | int(11) unsigned | NO | | NULL | |
| hire_date | date | NO | | NULL | |
| position | varchar(75) | NO | | NULL | |
| manager | varchar(75) | NO | | NULL | |
| employment_application | char(1) | NO | | i | |
| resume | char(1) | NO | | i | |
| references_checked | char(1) | NO | | i | |
| new_hire_letter | char(1) | NO | | i | |
| clinical_training_initiated | char(1) | NO | | i | |
| name_badge_ordered | char(1) | NO | | i | |
| keycode_access | char(1) | NO | | i | |
| tf_up_setup | char(1) | NO | | i | |
| un_pw_setup | char(1) | NO | | i | |
| drug_screen | char(1) | NO | | i | |
| background_investigation | char(1) | NO | | i | |
| licenses_education_verified | char(1) | NO | | i | |
| cpr | char(1) | NO | | i | |
| clinic_tour_introductions | char(1) | NO | | i | |
| sick_tardy_phone_notification | char(1) | NO | | i | |
| work_sched_start_time | char(1) | NO | | i | |
| paydays | char(1) | NO | | i | |
| ds_pto | char(1) | NO | | i | |
| attendance_tardiness | char(1) | NO | | i | |
| evacuation_plan | char(1) | NO | | i | |
| osha | char(1) | NO | | i | |
| emp_handbook_receipt | char(1) | NO | | i | |
| internet_email_phone_usage | char(1) | NO | | i | |
| parking_building_access | char(1) | NO | | i | |
| dress_grooming | char(1) | NO | | i | |
| inclement_weather | char(1) | NO | | i | |
| mileage | char(1) | NO | | i | |
| hipaa_training | char(1) | NO | | i | |
| direct_deposit_auth | char(1) | NO | | i | |
| i9 | char(1) | NO | | i | |
| w4 | char(1) | NO | | i | |
| valid_id | char(1) | NO | | i | |
| update_ext_list | char(1) | NO | | i | |
| emerg_contact_form | char(1) | NO | | i | |
| med_dent_life_vision | char(1) | NO | | i | |
| 401k_pen_sh | char(1) | NO | | i | |
| centricity_access | char(1) | NO | | i | |
| centricity_training | char(1) | NO | | i | |
| phone_training | char(1) | NO | | i | |
| create_active_dir_account | char(1) | NO | | i | |
| email_access | char(1) | NO | | i | |
| drive_access | char(1) | NO | | i | |
| mcafee | char(1) | NO | | i | |
| hr_enrollment_forms | char(1) | NO | | i | |
| ds_signoff | char(1) | NO | | i | |
| ds_clincial_signoff | char(1) | NO | | i | |
| completed | datetime | YES | | NULL | |
| created | datetime | NO | | NULL | |
| last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| portal_access | char(1) | NO | | i | |
| harvest | char(1) | NO | | i | |
| imms_link | char(1) | NO | | i | |
| eprescribe | char(1) | NO | | i | |
| state_alert_access | char(1) | NO | | i | |
| phone_agent | char(1) | NO | | i | |
| pc_tablet_setup | char(1) | NO | | i | |
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
61 rows in set (0.00 sec)


tableB:
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| staff_fk | int(11) unsigned | NO | | NULL | |
| employment_application | char(1) | NO | | i | |
| resume | char(1) | NO | | i | |
| references_checked | char(1) | NO | | i | |
| new_hire_letter | char(1) | NO | | i | |
| clinical_training_initiated | char(1) | NO | | i | |
| name_badge_ordered | char(1) | NO | | i | |
| keycode_access | char(1) | NO | | i | |
| tf_up_setup | char(1) | NO | | i | |
| un_pw_setup | char(1) | NO | | i | |
| drug_screen | char(1) | NO | | i | |
| background_investigation | char(1) | NO | | i | |
| licenses_education_verified | char(1) | NO | | i | |
| cpr | char(1) | NO | | i | |
| clinic_tour_introductions | char(1) | NO | | i | |
| sick_tardy_phone_notification | char(1) | NO | | i | |
| work_sched_start_time | char(1) | NO | | i | |
| paydays | char(1) | NO | | i | |
| ds_pto | char(1) | NO | | i | |
| attendance_tardiness | char(1) | NO | | i | |
| evacuation_plan | char(1) | NO | | i | |
| osha | char(1) | NO | | i | |
| emp_handbook_receipt | char(1) | NO | | i | |
| internet_email_phone_usage | char(1) | NO | | i | |
| parking_building_access | char(1) | NO | | i | |
| dress_grooming | char(1) | NO | | i | |
| inclement_weather | char(1) | NO | | i | |
| mileage | char(1) | NO | | i | |
| hipaa_training | char(1) | NO | | i | |
| direct_deposit_auth | char(1) | NO | | i | |
| i9 | char(1) | NO | | i | |
| w4 | char(1) | NO | | i | |
| valid_id | char(1) | NO | | i | |
| update_ext_list | char(1) | NO | | i | |
| emerg_contact_form | char(1) | NO | | i | |
| med_dent_life_vision | char(1) | NO | | i | |
| 401k_pen_sh | char(1) | NO | | i | |
| centricity_access | char(1) | NO | | i | |
| centricity_training | char(1) | NO | | i | |
| phone_training | char(1) | NO | | i | |
| create_active_dir_account | char(1) | NO | | i | |
| email_access | char(1) | NO | | i | |
| drive_access | char(1) | NO | | i | |
| mcafee | char(1) | NO | | i | |
| portal_access | char(1) | NO | | i | |
| harvest | char(1) | NO | | i | |
| imms_link | char(1) | NO | | i | |
| eprescribe | char(1) | NO | | i | |
| state_alert_access | char(1) | NO | | i | |
| phone_agent | char(1) | NO | | i | |
| pc_tablet_setup | char(1) | NO | | i | |
| hr_enrollment_forms | char(1) | NO | | i | |
| ds_signoff | char(1) | NO | | i | |
| ds_clincial_signoff | char(1) | NO | | i | |
| completed | datetime | YES | | NULL | |
| created | timestamp | NO | | CURRENT_TIMESTAMP | |
| last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
58 rows in set (0.01 sec)


I'll include the schemas if anyone would like to see them.

I'm wondering if there's a way to do this without explicitly labeling each column in the INSERT INTO statement. Any help will be greatly appreciated!

Answer

No, there is no way to do this without labeling each column. There is no syntax in SQL for SELECT *_except_for_a_few_columns

You can generate the list of columns out of the INFORMATION_SCHEMA so you can cut down on tedious typing:

SELECT GROUP_CONCAT(column_name ORDER BY ordinal_position) AS _cols
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'mydatabase' AND table_name = 'tableA'
  AND column_name NOT IN ('do', 'not', 'want')

To further complicate things, the second table does not have the columns in the same order as the first...

If the columns at least have the same names in both tables, you can use a column list in your INSERT statement regardless of the natural ordinal position of those columns defined in the table. In other words, the following works:

INSERT INTO tableB (col1, col4, col2, col6, col9)
  SELECT col1, col4, col2, col6, col9 FROM tableA;

If the columns differ in number, order, and name, I'd recommend that you double-check that you're inserting into the correct table! :-)

Comments