gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:09:57 24 4
gpt4 key购买 nike

我是一个非常新手,仍然使用 SQL,并且已经完成了一些看起来非常乏味的任务。我需要将 61 列中的 58 列从表 A 移动到表 B。 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? .问题是他们都从一张小 table 搬到一张大 table ,而不是面对面。

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

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 语句中的每一列的情况下执行此操作。任何帮助将不胜感激!

最佳答案

不,如果不标记每一列,就无法做到这一点。 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')

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

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

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

如果列的编号、顺序、名称不同,我建议您仔细检查是否插入到正确的表中! :-)

关于MySQL:将数据从 61 列表移动到 58 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39714211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com