将数据从不同表的行复制到另一行

时间:2016-10-20 09:54:09

标签: sql sqlite

我有这样的情况:

enter image description here

enter image description here

enter image description here

第三表想得到“C20 / 25”的fcd值和B400C的fyk值。

以下方法有效但不符合我的要求:

INSERT INTO Column(fck) 
    SELECT fck 
    FROM StrengthClassesForConcrete 
    WHERE nameClass = "C20/25";

INSERT INTO Column(fyk) 
    SELECT fyk 
    FROM StrengthClassesForSteel 
    WHERE nameClass = "B400C"

enter image description here

我希望他们添加到一行,而且表中只有一行。

3 个答案:

答案 0 :(得分:1)

我不知道你正在使用什么SQL实现。但你可以像这样进行连接(对于标准SQL):

INSERT INTO <table> (fck,fyk) SELECT StrengthClassesForConcrete.fck, StrengthClassesForSteel.fyk FROM StrengthClassesForConcrete, StrengthClassesForSteel WHERE StrengthClassesForConcrete.nameClass = "C20/25" AND StrengthClassesForSteel.nameClass = "B400C"
使用别名

或更短:

insert into <table> (fck,fyk) SELECT a.fck, b.fyk FROM StrengthClassesForConcrete a, StrengthClassesForSteel b WHERE a.nameClass = "C20/25" AND b.nameClass = "B400C"

<table>必须替换为所需表格/视图的名称

答案 1 :(得分:0)

您需要先使用交叉连接加入两个表,然后它将成为一个表,您将从该表中选择一行并将其插入表中。

答案 2 :(得分:0)

应该是这样的:

 INSERT INTO Column(fck,fyk) 
     SELECT StrengthClassesForConcrete.fck, StrengthClassesForSteel.fyk 
     FROM   StrengthClassesForConcrete, StrengthClassesForSteel 
     WHERE  StrengthClassesForConcrete.nameClass = "C20/25" 
     AND    StrengthClassesForSteel.nameClass = "B400C"