MYSQL 在插入查询中使用多个选择返回列数与第 1 行的值数不匹配

标签 mysql sql sql-update

更新

有时,当一个家庭从系统中被停用时,它可能包含不止一个人。在我的案例中,在 sql fiddle 中,household_id=12 的家庭有 3 个人。

我需要将这 3 个人的数据从 indiviudal 表中插入到 individual_history 表中,只需将 ind_action 字段更改为以下消息 HH 已被停用

这是一个示例数据:

| 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 表中的 SELECT 查询值插入到名为 individual_history 的表中。

这里是查询:

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 addeduser_id

systemDate 就是 now() 函数的结果。

我尝试使用 12 作为 hid 运行此查询,但收到以下错误:

1136 - Column count doesn't match value count at row 1

经过几次搜索,我 found that I should add parenthesis对于每个值。所以我将查询更改为:

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

还是报同样的错

tried to count the number of fields我正在插入的与我正在选择的相比,它们是相同的(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 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

near ' 'HH Status Changed to inactive', ' at line 10

请注意,两个表中字段的数据类型完全相同,individual_history 表包含一个自增主键。

HERE IS AN SQL FIDDLE检查示例数据。

最佳答案

对于您要尝试执行的操作,您不需要两个 SELECT。如果你想为 ind_action 使用一些特定的值,只需在你的选择中替换它,就像你对 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()

关于MYSQL 在插入查询中使用多个选择返回列数与第 1 行的值数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53315130/

相关文章:

sql - 使用 table2、table3 合并到 Table1(多个条件)?缺少语法

mysql - 在Java应用程序中使用JDBC并行读取大数据的标准算法或模式

mysql - Docker "Can' t 通过socket连接本地MySQL服务器"

php - MySQL 日期搜索 MM/DD/YYYY 到 MM/DD/YYYY

mysql - 如何在 WHERE 子句中使用 IF 语句

SQL Management Studio 将无法识别脚本创建后存在的表

sql - postgresql - 加入匹配的表以及不匹配的表?

mysql - 更新 MySQL 表,但只查询最大值

java - Java 中 'v_plateno' 中的未知列 'where clause'

mysql group_concat 作为 set 语句的值