![](/img/trans.png)
[英]Procedure to insert a data from one table to another table after calculations
[英]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.