【问题标题】:Copying rows via sql query通过sql查询复制行
【发布时间】:2014-02-08 06:12:50
【问题描述】:

我正在尝试通过 sql 查询插入多行 场景是这样的: 1 testID 有多个属性,我想通过提供 TestID 将一个测试的属性复制到另一个测试这是我正在尝试的查询,但它根本不起作用

INSERT INTO dc_tp_attributes
(Attribute_name,acronym,active,description,testId,
subdepartmentid,
DOrder,Attribute_type,D_A_formula,D_A_formula_desc,
Linesno,DefaultValue,interfaced,ClientID,Enteredby,
Enteredon,drived,parentid,heading,print,interfaceid)

select Attribute_name,acronym,active,description,
635,subdepartmentid,DOrder,Attribute_type,D_A_formula,
D_A_formula_desc,Linesno,DefaultValue,interfaced,
ClientID,Enteredby,Enteredon,drived,parentid,
heading,print,interfaceid
FROM dc_tp_attributes
Where testid=877

这里我将testID=877的属性复制到testID=635,ID为877的测试有10个属性,而testID 635的测试只有1个。 有一个名为 AttributeID 的 Autoincrement Primery Key,错误是

"Field 'AttributeID' doesn't have a default value"

【问题讨论】:

    标签: mysql sql batch-insert


    【解决方案1】:

    去掉SELECT中多余的括号:

    INSERT INTO dc_tp_attributes
    (Attribute_name,... interfaceid)
    
    select Attribute_name, ... interfaceid
    FROM dc_tp_attributes
    Where testid=877
    

    【讨论】:

    • 现在它说“Field AttributeID没有默认值”...在这个表中有一个名为Attribute id的自动增量主键
    • 奇怪 - 如果 AUTO_INCREMENT 列被排除在外,它应该添加带有 insert into ... select 的新 ID - 请参阅此 fiddle example。您确定 auto_increment 吗?例如我可以重复这个here
    • 它现在可以工作了,非常感谢你的帮助.. :)
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2011-03-03
    • 2021-11-12
    • 1970-01-01
    相关资源
    最近更新 更多