繁体   English   中英

在第一个表中插入数据后,如何将数据从一个表插入到另一个表?

[英]How can I insert data from one table to another table after insert a data in first table?

请帮我,

我怎样才能从一个表插入到另一个数据表中插入第一表格数据之后 我写了一个查询,但是有问题,它说“ Unknown column'test.ProjectNumber'”。 我应该先定义它吗? 我想从test2.PN插入数据到test.ProjectNumber请帮助我

码:

    /**
 * Insert a record on another table
 */

// SQL statement parameters
$insert_table  = 'test2';    
$insert_fields = array(  
     'test2.PN' => 'test.ProjectNumber',
 );

// Insert record
$insert_sql = 'INSERT INTO ' . $insert_table
    . ' ('   . implode(', ', array_keys($insert_fields))   . ')'
    . ' VALUES ('    . implode(', ', array_values($insert_fields)) . ')';

sc_exec_sql($insert_sql);

您的SQL评估如下:

INSERT INTO test2 ( test2.PN ) VALUES ( test.ProjectNumber );

这是无效的,因为在这种情况下无法理解test.ProjectNumber

我认为您需要做的是两个单独的插入语句,如下所示:

-- assumes that the number has not been inserted into test2 yet, 
-- just insert the same value into both tables:
INSERT INTO test2 ( test2.PN ) 
VALUES ( somevalue );

INSERT INTO test ( test.ProjectNumber )
VALUES ( somevalue );

或者如果已经插入了第一行(您的问题尚不清楚),则可以选择该值并将其插入,但是您需要能够识别该行(我正在使用123

-- If test2 already contains the row you need to insert from
-- then you need to select that row to insert the new row in test
INSERT INTO test ( test.ProjectNumber )
SELECT test2.PN
FROM test2
WHERE test2.Id = someid;

希望这会有所帮助-我没有为您翻译成php,部分是作为练习,但部分是因为我的php很生锈...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM