从更多相同的 table 中创建一个大的 table
Create one big table from more same tables
很久以前,我创建了一个应用程序,其中包含每个用户 table 的数据库,每个 table 只有 1 行。所以我有 table 用户 1、用户 2、...、用户 2800。现在我知道这是一个糟糕的主意,所以我正在尝试为每个用户创建一个大 table。我需要创建 table
CREATE TABLE `users` (`user_id`, `col1`, `col2`, ...)
来自 table 个赞:
TABLE `user###` (`row_id`, `col1`, `col2`, ...)
其中 ### 是用户的 ID。
请问有什么好的方法吗?
不要尝试在 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
}
它太宽泛了,无法确定您最喜欢的语言是什么,以及创建脚本的细节是什么,但这是您应该做的。
由于所有用户 table 都有相同的字段,您只需创建唯一的 table 然后遍历所有用户 table 来检索他们的内容。
例如 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);
}
很久以前,我创建了一个应用程序,其中包含每个用户 table 的数据库,每个 table 只有 1 行。所以我有 table 用户 1、用户 2、...、用户 2800。现在我知道这是一个糟糕的主意,所以我正在尝试为每个用户创建一个大 table。我需要创建 table
CREATE TABLE `users` (`user_id`, `col1`, `col2`, ...)
来自 table 个赞:
TABLE `user###` (`row_id`, `col1`, `col2`, ...)
其中 ### 是用户的 ID。
请问有什么好的方法吗?
不要尝试在 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
}
它太宽泛了,无法确定您最喜欢的语言是什么,以及创建脚本的细节是什么,但这是您应该做的。
由于所有用户 table 都有相同的字段,您只需创建唯一的 table 然后遍历所有用户 table 来检索他们的内容。
例如 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);
}