2013-03-14 47 views
0

請有人可以幫助我一直在這個查詢幾個小時。我想從我的表ptb_registrations複製某些列,並將它們作爲新行插入到ptb_users中。複製表a中的某些列並插入到表b中?

我ptb_registrations表看起來像這樣:

id | username | password | firstname | lastname | email 

1  dave  password  james   tanner  [email protected] 
2  jonx10  gundam00  eric   davies  [email protected] 
3  33csgge  henry10  daniel   gilmart  [email protected] 
這些列中,我只需要插入ID,名字,姓氏,電子郵件地址和密碼不是username

所以我ptb_user表看起來像這樣:

id | user_id | password | firstname | lastname | email 

    1  1  password  james   tanner  [email protected] 
    2  2  gundam00  eric   davies  [email protected] 
    3  3  henry10  daniel   gilmart  [email protected] 

ID是自動增量值,我想USER_ID有當數據被插入過(如果是相同的值auto_incremented id列因此,其中id是1 - user_id也將爲1)

到目前爲止,我已經嘗試了不同的方式獲取從ptb_registrations插入到ptb_users的列的負載,但沒有任何工作適合我。

罐頭有人請告訴我我錯在哪裏,謝謝。

// Make a safe query 
    $query ="insert into ptb_users(id, first_name) 
    select ptb_registrations.id, ptb_registrations.firstname 
    from ptb_registrations, temp 
    where ptb_users.id=temp.id; 
    "; 

    mysql_query($query)or die('Could not update members: ' . mysql_error()); 
+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。 – Kermit 2013-03-14 19:07:49

回答

0

有沒有辦法保證你的新AUTO_INCREMENT將以前的用戶ID的匹配...如果序列中會發生什麼情況?你最好的選擇是在插入記錄後重新分配新表的ID。

你的查詢應該是這樣的:

INSERT INTO ptb_user (user_id, password, firstname, lastname, email) 
SELECT id, password, firstname, lastname, email 
FROM ptb_registrations 
ORDER BY id 

附:我希望你不要將密碼保存在明文

相關問題