在insert查询中使用多个selects的mysql返回的列计数与第1行的值计数不匹配

kq0g1dla  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(320)

更新
有时,当一个家族从一个系统中被灭活时,它可能包含一个以上的个体。在sql fiddle的例子中,家庭id=12的家庭有3个人。
我需要插入这3个人的数据作为相同的从 indiviudal 表到 individual_history 换张table ind_action 字段添加到以下消息中 HH has been inactivated .
以下是数据示例:

| individual_id | household_id | family_relation_id | marital_status_id | ind_lmms_id | ind_un_id | head_of_hh | ind_first_name_ar | ind_last_name_ar | ind_first_name_en | ind_last_name_en | ind_gender |        dob | ind_status |       ind_date_added | user_id |          system_date |
|---------------|--------------|--------------------|-------------------|-------------|-----------|------------|-------------------|------------------|-------------------|------------------|------------|------------|------------|----------------------|---------|----------------------|
|             1 |           12 |                  3 |                 1 |         321 |    (null) |         no |                 u |                x |            (null) |           (null) |       Male | 2012-01-01 |     Active | 2018-07-19T00:00:00Z |       1 | 2018-07-19T00:00:00Z |
|             2 |           12 |                  1 |                 2 |         123 |    (null) |         no |                 x |                y |            (null) |           (null) |     Female | 1998-03-05 |     Active | 2015-03-05T00:00:00Z |       1 | 2015-03-05T00:00:00Z |
|             3 |           12 |                  3 |                 1 |        1234 |    (null) |         no |                 x |                z |            (null) |           (null) |     Female | 2004-04-05 |     Active | 2018-04-11T00:00:00Z |       1 | 2018-04-11T00:00:00Z |

所有3个字段都应插入到表中 individual_history 以及 ind_action 设置为我上面添加的注解。
我需要插入一个叫做 individual_history a的值 SELECT 从表中查询 individual .
以下是查询:

INSERT INTO individual_history 
            (individual_id, 
             household_id, 
             family_relation_id_history, 
             marital_status_id_history, 
             ind_lmms_id_history, 
             ind_un_id_history, 
             head_of_hh_history, 
             ind_first_name_ar_history, 
             ind_last_name_ar_history, 
             ind_first_name_en_history, 
             ind_last_name_en_history, 
             ind_gender_history, 
             dob_history, 
             ind_status_history, 
             ind_action, 
             ind_date_changed, 
             user_id, 
             system_date) 
VALUES      ((SELECT i.individual_id, 
                     i.household_id, 
                     i.family_relation_id, 
                     i.marital_status_id, 
                     i.ind_lmms_id, 
                     i.ind_un_id, 
                     i.head_of_hh, 
                     i.ind_first_name_ar, 
                     i.ind_last_name_ar, 
                     i.ind_first_name_en, 
                     i.ind_last_name_en, 
                     i.ind_gender, 
                     i.dob, 
                     i.ind_status 
              FROM   individual i 
              WHERE  i.household_id = :hid), 
             'HH Status Changed to inactive', 
             (SELECT i.ind_date_added, 
                     i.user_id 
              FROM   individual i 
              WHERE  i.household_id = :hid), 
             :systemDate)

从查询中可以看到,我正在拆分 SELECT 语句分成两部分,因为我想插入一个特定的 ind_action 消息,然后我将继续获取其他2个字段 date added 以及 user_id .
这个 systemDate 只是 now() 函数结果。
我试着用12来运行这个查询 hid 我收到了以下错误:

1136-列计数与第1行的值计数不匹配

在进行了几次搜索之后,我发现应该为每个值添加括号。所以我把查询改为:

INSERT INTO individual_history 
            (individual_id, 
             household_id, 
             family_relation_id_history, 
             marital_status_id_history, 
             ind_lmms_id_history, 
             ind_un_id_history, 
             head_of_hh_history, 
             ind_first_name_ar_history, 
             ind_last_name_ar_history, 
             ind_first_name_en_history, 
             ind_last_name_en_history, 
             ind_gender_history, 
             dob_history, 
             ind_status_history, 
             ind_action, 
             ind_date_changed, 
             user_id, 
             system_date) 
VALUES      ((SELECT i.individual_id, 
                     i.household_id, 
                     i.family_relation_id, 
                     i.marital_status_id, 
                     i.ind_lmms_id, 
                     i.ind_un_id, 
                     i.head_of_hh, 
                     i.ind_first_name_ar, 
                     i.ind_last_name_ar, 
                     i.ind_first_name_en, 
                     i.ind_last_name_en, 
                     i.ind_gender, 
                     i.dob, 
                     i.ind_status 
              FROM   individual i 
              WHERE  i.household_id = 12), 
             ( 'HH Status Changed to inactive' ), 
             (SELECT i.ind_date_added, 
                     i.user_id 
              FROM   individual i 
              WHERE  i.household_id = 12), 
             ( NOW() ))

但还是有同样的错误。
我试着计算我插入的字段和我选择的字段的数量,它们是相同的(18个字段)。
更新
我通过删除 VALUES 条款:

INSERT INTO individual_history 
            ( 
                        individual_id, 
                        household_id, 
                        family_relation_id_history, 
                        marital_status_id_history, 
                        ind_lmms_id_history, 
                        ind_un_id_history, 
                        head_of_hh_history, 
                        ind_first_name_ar_history, 
                        ind_last_name_ar_history, 
                        ind_first_name_en_history, 
                        ind_last_name_en_history, 
                        ind_gender_history, 
                        dob_history, 
                        ind_status_history, 
                        ind_action, 
                        ind_date_changed, 
                        user_id, 
                        system_date 
            ) 
SELECT i.individual_id, 
       i.household_id, 
       i.family_relation_id, 
       i.marital_status_id, 
       i.ind_lmms_id, 
       i.ind_un_id, 
       i.head_of_hh, 
       i.ind_first_name_ar, 
       i.ind_last_name_ar, 
       i.ind_first_name_en, 
       i.ind_last_name_en, 
       i.ind_gender, 
       i.dob, 
       i.ind_status 
FROM   individual i 
WHERE  i.household_id=12, 
       'HH Status Changed to inactive', 
       ( 
              SELECT i.ind_date_added, 
                     i.user_id 
              FROM   individual i 
              WHERE  i.household_id=12), 
       now()

我得到了以下错误:

1064-您的sql语法有错误;检查与mysql服务器版本相对应的手册,以获得正确的语法

第10行“hh状态变为非活动”附近
请注意,两个表中字段的数据类型完全相同,并且 individual_history 表包含自动递增主键。
这里有一个sql fiddle来检查示例数据。

wqnecbli

wqnecbli1#

@马丁亨宁斯,我有点晚了。。。但是这个查询应该是有效的:

INSERT INTO individual_history 
        (individual_id, 
         household_id, 
         family_relation_id_history, 
         marital_status_id_history, 
         ind_lmms_id_history, 
         ind_un_id_history, 
         head_of_hh_history, 
         ind_first_name_ar_history, 
         ind_last_name_ar_history, 
         ind_first_name_en_history, 
         ind_last_name_en_history, 
         ind_gender_history, 
         dob_history, 
         ind_status_history, 
         ind_action, 
         ind_date_changed, 
         user_id, 
         system_date)
  SELECT individual_id, 
         household_id, 
         family_relation_id, 
         marital_status_id, 
         ind_lmms_id, 
         ind_un_id, 
         head_of_hh, 
         ind_first_name_ar, 
         ind_last_name_ar, 
         ind_first_name_en, 
         ind_last_name_en, 
         ind_gender, 
         dob, 
         ind_status,
         'HH Status Changed to inactive',
         ind_date_added,
         user_id,
         now()
  FROM   individual
 WHERE   individual.household_id = 12
iaqfqrcu

iaqfqrcu2#

你不需要两个选择来做你想做的事。如果你想用一些特定的值 ind_action ,只需在select中替换它,与使用 now() 功能:

INSERT INTO targetTable (col1, col2, col3, col4, colTime)
    SELECT colA, colB, 'my specific string', colD, now()
    FROM sourceTable WHERE colA = 12;

在这里, col3 获取字符串, colTime 这个 now() .

相关问题