2017-02-13 42 views
0

很久以前,我創建了包含每個用戶的數據庫與表的應用程序,每個表只有1行。所以我有像user1,user2,...,user2800這樣的表格。現在我知道這是一個可怕的想法,所以我試圖爲每個用戶創建一張大桌子。我需要創建表從更多相同的表中創建一個大表

CREATE TABLE `users` (`user_id`, `col1`, `col2`, ...) 

從象表:

TABLE `user###` (`row_id`, `col1`, `col2`, ...) 

其中###是用戶ID。

有沒有什麼好的方法如何做到這一點?

+0

Thse倍數用戶表不具有相同的字段? – Veve

+0

@Veve每個表格都有相同的字段 –

+0

您可以使用類似PHP的任務來完成此任務嗎? –

回答

1

由於所有用戶表具有相同的字段,因此您只需創建唯一表,然後遍歷所有用戶表以檢索其內容。

例如,使用PHP,它可能是這樣的:

$sql = CREATE TABLE `users` (`user_id`, `col1`, `col2`, `col3`); 
$mysqli_query($link, $sql); 

$nbContacts = 2800; 
for($i = 1;$i <= $nbContacts; $i++) 
{ 
    $sql = 'INSERT INTO users(`user_id`, `col1`, `col2`, `col3`) SELECT `'.$i.'`, `col1`, `col2`, `col3` FROM `user'.$i.'`'; 
    $mysqli_query($link, $sql); 
} 
1

不要試圖在sql中這樣做,只需使用您最喜歡的語言編寫腳本。在僞代碼:

$tables = get_tables(); // write funciton that uses "show tables from books;" 
foreach $tables as $table { 
    $userid = getIdFromTableName($table); // simple regexp 
    $data = getRowFromTable($table); // get data from old table 
    insertIntoUser($userid, $data); // inserts your userid and the data in the new table 
} 

它太廣泛,找出你最喜歡的語言是什麼,以及具體創建腳本,但是這是你應該做的。

相關問題