2012-04-04 55 views
0

我更新了我的程序處理罰款和收費的方式,現在我需要將舊數據傳輸/遷移到新格式。過去工作的方式是罰款/費用存儲在用戶記錄的一個字段中。現在有一個全新的表格用於跟蹤每次充電。我想簡單地將該費用字段和1條記錄帶到費用表中。mysql傳輸查詢

User table: id as INT, name as VARCHAR, charges as DECIMAL(10, 2) 
Charge Table: id as INT, user_id as INT, amount as DOUBLE, type as INT, date_applied as DATETIME, description as VARCHAR 

可以說我有一個用戶記錄:(id => 1, name => 'john doe', charges => 4.20)並希望該轉移到的

(id => 1, user_id => 1, amount => 4.20, type => 1, date_applied => (whatever time the transfer is taking place), description => 'Initial balance') 

收費記錄我怎樣才能做一個SQL查詢這一次轉移?我寧願不寫這個一次性交易的PHP腳本。

回答

2

這應該做你想做的。

INSERT INTO `Charge Table` SELECT NULL, id, charges, 1, NOW(), 'Initial balance' FROM `User Table` 
+0

因此,如果我理解正確,這應該工作? (SELECT,NULL,id,fines,null,'Initial balance',NOW(),1 FROM'patrons') – LordZardeck 2012-04-04 18:08:21

+0

實際上,那沒有。我試過了,並且在'SELECT NULL,id,fines,null, – LordZardeck 2012-04-04 18:15:03

+0

'附近出現錯誤,不需要'VALUES'或者parens。你可以只是'INSERT INTO賬單(id,patron_id,amount,item_id,description,date_charged,type)SELECT NULL,id,fines,null,'Initial balance',NOW(),1 FROM patrons' – 2012-04-04 18:15:51