简体   繁体   English

将不存在的行从一个数据库表复制到另一数据库表?

[英]Copying non existing rows from one database table to another database table?

I have the following 1 db table in Database 1 and 1db table in Database 2, now the stucture of both tables are exactly the same. 我在数据库1中有以下1 db表,在数据库2中有1 db表,现在两个表的结构完全相同。 Now what happens is table 1 (DB1) gets new rows added daily, I need to update the table 1 (DB 1) new rows in table 1 (DB 2) so that these 2 tables remain the same. 现在发生的是表1(DB1)每天都会添加新行,我需要在表1(DB 2)中更新表1(DB 1)的新行,以使这两个表保持不变。 A cron will trigger a php script on midnight to do this task. cron会在午夜触发php脚本来执行此任务。 What is the best way to do this and how using PHP/mysql? 最好的方法是什么?如何使用PHP / mysql?

You might care to have a look at replication (see http://dev.mysql.com/doc/refman/5.4/en/replication-configuration.html ). 您可能需要看一下复制(请参阅http://dev.mysql.com/doc/refman/5.4/en/replication-configuration.html )。 That's the 'proper' way to do it; 这是做到这一点的“正确”方法。 it isn't to be trifled with, though, and for small tables the above solutions are probably better (and certainly easier). 但是,这并不是一件小事,对于较小的表,上述解决方案可能更好(当然也更容易)。

This might help you out, its what i do on my database for a similar kinda thing 这可能会帮到您,这是我在数据库上为类似事情所做的工作

    $dropSQL = "DROP TABLE IF EXISTS `$targetTable`";
    $createSQL = "CREATE TABLE `$targetTable` SELECT * FROM `$activeTable`";
    $primaryKeySQL = "ALTER TABLE `$targetTable` ADD PRIMARY KEY(`id`)";
    $autoIncSQL = "ALTER TABLE `$targetTable` CHANGE `id` `id` INT( 60 ) NOT NULL  AUTO_INCREMENT";
    mysql_query($dropSQL);
    mysql_query($createSQL);
    mysql_query($primaryKeySQL);
    mysql_query($autoIncSQL);

obviously you will have to modify the taget and active table variables. 显然,您将不得不修改标记和活动表变量。 Dropping the table will lose the primary key when you do this, oh well .. easy enough to add back in 删除表格时,这样做会丢失主键,哦..足够容易添加回来

I would recommend replication as has already been suggested. 我建议像已经建议的那样进行复制。 However, another option is to use mysqldump to grab the rows you need and send them to the other table. 但是,另一种选择是使用mysqldump来获取所需的行并将其发送到另一个表。

mysqldump -uUSER -pPASSWORD -hHOST --compact -t --where="date=\"CURRENT_DATE\"" DB1 TABLE | mysql -uUSER -pPASSWORD -hHOST -D DB2

Replace USER , HOST , and PASSWORD with login info for your database. USERHOSTPASSWORD替换为数据库的登录信息。 You can use different information for each part of the command if DB1 and DB2 have different access information. 如果DB1DB2具有不同的访问信息,则可以为命令的每个部分使用不同的信息。 DB1 and DB2 are the names of your databases, and TABLE is the name of the table. DB1DB2是数据库的名称,而TABLETABLE的名称。

You can also modify the --where option to grab only the rows which need to updated. 您也可以修改--where选项以仅获取需要更新的行。 Hopefully you have some query you can use. 希望您可以使用一些查询。 As mentioned previously, if the table has a primary key, you could grab the last key which DB2 has using a command something like 如前所述,如果表具有主键,则可以使用以下命令来获取DB2拥有的最后一个键:

KEY=`echo "SELECT MAX(KEY_COLUMN) FROM TABLE;" mysql -uUSER -pPASSWORD -hHOST -D DB2`

for a bash shell script (then use this value in the WHERE clause above). bash shell脚本(然后在上面的WHERE子句中使用此值)。 Depending on how your primary key is generated, this may be a bad idea since rows may be added in holes in the keyspace if they exist. 根据主键的生成方式,这可能不是一个好主意,因为如果行存在,则可能会将行添加到键空间的孔中。

This solution will also work if rows are changed as long as you have a query which can select these rows. 如果更改行,只要您有一个可以选择这些行的查询,此解决方案也将起作用。 Just add the --replace option to the mysqldump command. 只需将--replace选项添加到mysqldump命令即可。 In your situation, it would be best to add some type of value such as date updated which you can compare by. 根据您的情况,最好添加某种类型的值,例如可以用来比较的更新日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将行从一个表复制到另一个表时出现问题 - Issue in copying rows from one table to another 如何将表的行从一个数据库插入到另一个数据库(使用两个PDO) - How to insert rows of a table from one database to another (with two PDO) 在 Laravel 上将数百万行数据从一个数据库复制到另一个数据库 - Copying million rows of data from one database to another on laravel mysql使用php将特定列从一个数据库表复制到另一个数据库表 - mysql copying specific columns from one database table to another using php 在 Laravel 中使用一个项目的数据库的现有表到另一个项目 - Use an existing table of a database of one project to another project in Laravel Laravel从数据库表到另一个表的1mill行,速度很慢 - Laravel from database table to another table 1mill rows, slow 如何将mysql数据库中的选定值与另一个表中的现有值进行比较? - How to compare a selected value from mysql database with an existing one in another table? 数据库-查询以检索另一个表中不存在的行 - Database - Query to retrieve rows not existing in the other table 如何将表从一个mysql数据库复制到另一个mysql数据库 - How to copy a table from one mysql database to another mysql database 如何检查数据库表中的现有行 - How to check for existing rows in a database table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM