简体   繁体   English

如何从 MySQL 中的 2 个表中复制新数据但跳过旧数据

[英]How to copy new data but skip old data from 2 tables in MySQL

I am very new to SQL (like today new) and I have a question.我对 SQL非常陌生(就像今天一样新),我有一个问题。 I'm trying to copy data from one table into another table.我正在尝试将数据从一个表复制到另一个表中。 I know how to do this from the internet but my issue is, I need to skip any data that is already there.我知道如何从互联网上做到这一点,但我的问题是,我需要跳过任何已经存在的数据。 Here's my scenario.这是我的场景。 We have monitoring software that logs new users every time it encounters one.我们有监控软件,可以在每次遇到新用户时记录新用户。 We have an old database with thousands of users that we use as our database, but we had to switch to a different database for a short time due to a server outage.我们有一个包含数千个用户的旧数据库,我们将其用作我们的数据库,但由于服务器中断,我们不得不在短时间内切换到另一个数据库。 This means that we have some users on the new database, but most on the old.这意味着我们有一些用户在新数据库上,但大多数在旧数据库上。 There are also duplicates since everyone that was logged in the old had to be re-logged in the new when the software encountered a 'new user' that wasn't in the new database.还有重复,因为当软件遇到不在新数据库中的“新用户”时,必须重新登录旧版本的每个人都必须重新登录新版本。

What I want to do is copy all the new users in the new database over to the old database, but skip any users that are already in the old database.我想要做的是将新数据库中的所有新用户复制到旧数据库中,但跳过旧数据库中已经存在的任何用户。

How can this be done?如何才能做到这一点? Sorry for the long confusing paragraph.对于冗长的混乱段落感到抱歉。 Please remember I'm very new to SQL and don't know squat.请记住,我是 SQL 的新手,不知道深蹲。 Thanks in advance.提前致谢。

Edit: more info: Here is how my tables are set up.编辑:更多信息:这是我的表格的设置方式。 I have 2 databases 'bat' and 'bat2'.我有 2 个数据库“bat”和“bat2”。 bat2 is the new one, bat is the old one. bat2 是新的,bat 是旧的。

+---------------+
| Tables_in_bat |
+---------------+
| BAT_ban       |
| BAT_kick      |
| BAT_mute      |
| BAT_players   |
| BAT_web       |
| bat_ban       |
| bat_comments  |
| bat_kick      |
| bat_mute      |
| bat_players   |
| bat_web       |
+---------------+

+----------------+
| Tables_in_bat2 |
+----------------+
| BAT_ban        |
| BAT_kick       |
| BAT_mute       |
| BAT_players    |
| bat2_comments  |
+----------------+

I will need to merge BAT_players from the new one into BAT_players in the old one.我需要将新版本中的 BAT_players 合并到旧版本中的 BAT_players 中。 the lowercase tables are a different story so ignore those.小写表格是另一回事,因此请忽略它们。

The structure of the BAT_players table looks like this: BAT_players 表的结构如下所示:

+------------------+----------------------------------+-----------------+---------------------+---------------------+
| BAT_player       | UUID                             | lastip          | firstlogin          | lastlogin           |
+------------------+----------------------------------+-----------------+---------------------+---------------------+
| 00meow9          | 21826e64a2974911a0fe542bf11e3901 | 73.163.22.154   | 2021-05-06 10:49:49 | 2021-05-06 10:49:49 |
| 0800Nummer       | bb7bd784c7804834b2df56a6c76d53da | 85.250.79.8     | 2021-05-04 13:43:28 | 2021-05-08 08:04:33 |
| 1angelfish       | 6c7ebb3197d04d71a7976bd830abe452 | 68.131.116.38   | 2021-05-03 20:46:36 | 2021-05-07 17:32:15 |

The UUID is something that will stay consistent throughout the 2 files. UUID 将在 2 个文件中保持一致。

What I want to do is copy all the new users in the new database over to the old database, but skip any users that are already in the old database.我想要做的是将新数据库中的所有新用户复制到旧数据库中,但跳过旧数据库中已经存在的任何用户。

You need one or more columns to specify what a "user" is.您需要一列或多列来指定“用户”是什么。 One method if you have a user_id is:如果您有user_id ,一种方法是:

insert into old ( . . . )      -- list the columns here
    select . . .
    from new n
    where not exists (select 1 from old o where o.user_id = n.user_id);

MySQL has convenient construct, if you have a unique index or constraint on user_id (or whatever columns define the user): MySQL 具有方便的构造,如果您对user_id (或任何定义用户的列)有唯一索引或约束:

insert into old ( . . . )
    select . . .
    from new n
    on duplicate key update set user_id = values(user_id);

The most recent versions have deprecated VALUES() to you might get a warning.最新版本已弃用VALUES() ,您可能会收到警告。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM