gpt4 book ai didi

php - 从多个数据库导出表并导入到单个数据库

转载 作者:行者123 更新时间:2023-11-29 20:52:49 24 4
gpt4 key购买 nike

我想从多个数据库中的三个同名表中导出特定数据并将它们导入到单个数据库SUPPORT_DATABASE中,这些表是XXX_usersXXX_usergroupsXXX-user_usergroup_map

用户id是所有表中的主键。

这是我到目前为止的脚本

MySQL-Export.php

<?php
//ENTER THE RELEVANT INFO BELOW
$mysqlDatabaseName ='db1';
$mysqlUserName ='db1owner';
$mysqlPassword ='Password';
$mysqlHostName ='db1.mywebhost.net';
$mysqlExportPath ='db1backup.sql';

//DONT EDIT BELOW THIS LINE
//Export the database and output the status to the page
$command='mysqldump --opt -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .'
SELECT id, name, username, password, email FROM `xxx_users` WHERE `id` in (SELECT `user_id` FROM `xxx_user_usergroup_map` WHERE `group_id` = (SELECT `id` FROM `xxx_usergroups` WHERE `title` = 'Administrator'));
SELECT id, name, username, password, email FROM `xxx_users` WHERE `id` in (SELECT `user_id` FROM `xxx_user_usergroup_map` WHERE `group_id` = (SELECT `id` FROM `xxx_usergroups` WHERE `title` = 'Engineer'));
> ~/' .$mysqlExportPath;
exec($command,$output=array(),$worked);
switch($worked){
case 0:
echo 'Database <b>' .$mysqlDatabaseName .'</b> successfully exported to <b>~/' .$mysqlExportPath .'</b>';
break;
case 1:
echo 'There was a warning during the export of <b>' .$mysqlDatabaseName .'</b> to <b>~/' .$mysqlExportPath .'</b>';
break;
case 2:
echo 'There was an error during export. Please check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN </b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr></table>';
break;
}
?>

MySQL-Import.php

$mysqlDatabaseName ='db2';
$mysqlUserName ='db2owner';
$mysqlPassword ='Password';
$mysqlHostName ='db2.mywebhost.net';
$mysqlImportFilename ='db1backup.sql';

//DONT EDIT BELOW THIS LINE
//Export the database and output the status to the page
$command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename;
exec($command,$output=array(),$worked);
switch($worked){
case 0:
echo 'Import file <b>' .$mysqlImportFilename .'</b> successfully imported to database <b>' .$mysqlDatabaseName .'</b>';
break;
case 1:
echo 'There was an error during import. Please make sure the import file is saved in the same folder as this script and check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr><tr><td>MySQL Import Filename:</td><td><b>' .$mysqlImportFilename .'</b></td></tr></table>';
break;
}
?>

表结构

XXX_user
user_id, name, username, email, password
300, john smtih, jsmith, jsmith@test.com, password123@

XXX_usergroups
group_id, type
8, administrator

XXX_user_group_map
user_id, group_id
300, 8

我认为我可以做的是使用MySQL-Export.php从三个数据库导出数据并将数据库存储在服务器上,然后使用MySQL-import导入数据库。 php,从所有三个数据库到一个辅助数据库,一旦数据在这里MODIFYXXX-user_usergroup_map中的用户group_id到匹配 SUPPORT_DATABASE

中的用户组

此时我有点卡住了,因为从多个数据库导入用户,用户可能共享相同的user_id,所以我不确定该怎么做,在哪里以及如何更改user_id

一旦位于 SUPPORT_DATABASE 上,user_id 是什么并不重要

任何人都可以提出一些建议吗?

最佳答案

这是一种逐步实现的方法

  1. 使用此查询转储每个数据库

从“XXX_user”u、“XXX_user_group_map”g、“XXX_usergroups”g2 中选择 u.name、u.username、u.email、u.password、g2.type,其中 g.user_id = u.user_id AND g.group_id = g2.group_id

在此查询中,您将获得字符串形式的用户组类型,您可以使用该类型与您的 SUPPORT_DATABASE 组 ID 进行匹配

  • 现在编写一个脚本,将 SQL 导入到 SUPPORT_DATABASE 中,将 SUPPORT_DATABASE 设置为 user_id 上具有自动增量功能,并且在电子邮件上具有唯一性。
  • 关于php - 从多个数据库导出表并导入到单个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37892909/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com