MySQL:将数据从61列表移动到58列表

时间:2016-09-27 00:14:59

标签: mysql

我仍然是一个非常漂亮的绿色新手,他的任务仍然非常繁琐。我需要将61个列中的58个从tableA移动到tableB。 tableA有61列,只有15行,tableB现在有58列,零行。我已经阅读了多个关于如何实现这一目标的来源(将数据从一个表格移动到相关的表格),但没有一个具体谈论我正在处理的这个特殊情况。

为了使事情进一步复杂化,第二个表没有与第一个表的顺序相同的列,因此这也带来了问题。这篇SO帖子似乎很有希望,但它正在使用一个小得多的数据集:SQL: Move column data to other table in same relation。这一个也类似MYSQL - Move data from one table to a related one?。问题是他们都从一张较小的桌子搬到一张较大的桌子,而不是相对于他们。

这是我尝试进行此转换的初始查询(并且由于包含的错误指示不成功):

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)

如果有人想看到它们,我会包含模式。

我想知道是否有办法在没有明确标记INSERT INTO语句中的每一列的情况下执行此操作。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

不,没有标记每一列就没有办法做到这一点。 SQL中没有SELECT *_except_for_a_few_columns

的语法

您可以生成INFORMATION_SCHEMA中的列列表,这样您就可以减少繁琐的输入:

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')
  

为了使事情更复杂,第二个表没有与第一个表相同的列...

如果两列中的列至少具有相同的名称,则可以在INSERT语句中使用列列表,而不管表中定义的那些列的自然序号位置如何。换句话说,以下工作:

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

如果列数,订单,名称不同,我建议您仔细检查您是否正在插入正确的表格! : - )

答案 1 :(得分:1)

是的并且该错误非常简单,并且表示您无法使用*并且需要明确指定列名称

INSERT INTO tableB 
SELECT col1, col2, col3, ..., col58 FROM tableA;