2016-09-27 103 views
3

我是一個漂亮的綠色新手,仍然是SQL,並且任務非常繁瑣。我需要將tableA中61列中的58列移到tableB中。 tableA有61列,只有15行,tableB現在有58列和零行。我已經閱讀了關於如何實現這一點的多個源文件(將數據從一個表格移動到相關表格),但沒有一個具體談論我正在處理的這個特定情況。MySQL:將數據從61列表移動到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語句中明確標註每一列。任何幫助將不勝感激!

+0

nah。只需放上一個好的播放列表並剪切並粘貼名稱即可。 – Drew

+0

Bill Karwin您看到的結果來自「describe tableA」和「describe tableB」命令。我向你保證,模式分別是61列和58列。我將編輯問題以包含這些命令的完整結果。 – TomJ

+0

啊,以前不清楚你是在查詢'describe'而不是'select'。對於在您寫回復時刪除我的問題,我表示抱歉。 –

回答

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語句中使用列列表,而不管自然順序位置如何on表中定義的那些列。換句話說,以下工作:

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

如果列數,順序,名稱有所不同,我建議你仔細檢查你插入正確的表! :-)

+0

尼斯徹底的周到答案 – Drew

+0

正是我在找的東西!在意識到列計數產生的混淆之後,我在原始問題中發佈了模式。在原始表中只有15行,我真的很希望有一個更有效的方法來完成這個任務。謝謝你的偉大答案!我接受了它。 – TomJ

1

是那錯誤是非常簡單的,並說,你不能使用*和需要明確指定像

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

這就是我所害怕的,正如你所說的,語法錯誤非常清楚。雖然真的希望有某種祕密的SQL黑魔法! – TomJ

+0

@TomJ,黑魔法已經被*黑魔法師拼寫*見其他答案。 – Rahul

相關問題