SQL查询INSERT列计数错误

时间:2016-11-14 17:14:38

标签: mysql sql insert

我已从此查询中删除' age_range'

INSERT INTO `filters` (`id`, `user_id`, `profession_preference`, `country`, `city`, `order_by`, `profession_orientation`, `age_range`, `distance_range`, `location_dating`) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '18,23', '0,500 ', 0),
(26, 316, 3, 'United States', '', 3, 1, '17,25', '0,500', 0);

我再次执行并收到此错误:

  

MySQL说:文档

     

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

5 个答案:

答案 0 :(得分:2)

插入记录时,它会将VALUES列表中的值与列表中的列按逗号分隔的位置匹配。所以,这个插入语句:

INSERT INTO `foo` (`A`, `B`, `C`)
VALUES ('valueA', 'valueB', 'valueC')

它会将valueA插入列AvalueB插入列B等,因为它们会匹配各自列表中的位置。如果您从列列表中删除B并单独留下VALUES,则不会尝试将valueA插入列A,而valueB插入列{{} 1}}因为它们现在匹配值位置,但它不知道如何处理C因为现在有更多的值而不是列,所以既然你从第二个位置删除了列,你还需要从第二个位置删除该值。

回到您的查询,您需要确定列列表中占用的位置valueC,并从age_range列表中的相同位置删除值。

这有意义吗?

答案 1 :(得分:1)

您在insert语句中定义了9列,并且您尝试插入10个值。您需要添加其他列定义或从值中删除。

答案 2 :(得分:1)

enter image description here

根据规则,列名定义和提供的值count应该相同。在你的情况下,额外的一个列值。

答案 3 :(得分:0)

正如文件所说

  

"列数与值计数"

不匹配

您指定了9列(iduser_idprofession_preferencecountrycityorder_byprofession_orientation,插入语句中的distance_rangelocation_dating) 并且您正在尝试插入10个值。

您必须删除一个值或添加另一个列

答案 4 :(得分:0)

在删除列之前,这是可以使用的脚本

CREATE TABLE filters (id INT, user_id INT, profession_preference INT, country VARCHAR(50), city VARCHAR(50), order_by INT, profession_orientation INT, age_range VARCHAR(50), distance_range VARCHAR(50), location_dating INT);

INSERT INTO filters (id, user_id, profession_preference, country, city, order_by, profession_orientation, age_range, distance_range, location_dating) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '18,23', '0,500 ', 0),
(26, 316, 3, 'United States', '', 3, 1, '17,25', '0,500', 0);

现在,您删除了age_range列,下面的脚本将起作用:

INSERT INTO filters (id, user_id, profession_preference, country, city, order_by, profession_orientation, distance_range, location_dating) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '0,500', 0),
(26, 316, 3, 'United States', '', 3, 1, '0,500', 0);

我从插入脚本中删除了第三列。

希望这有帮助!