简体   繁体   English

使用LEFT JOIN将数据从一个数据库迁移到另一个数据库

[英]Migrating data from one database to another using LEFT JOIN

Let me start by saying I am relatively new to MySQL. 首先,我要说我是MySQL的新手。 I am trying to migrate data from one database to another. 我正在尝试将数据从一个数据库迁移到另一个数据库。 Let us call one database DB1 and the other DB2. 让我们将一个数据库称为DB1,将另一个称为DB2。

DB2 has tables the following tables. DB2的表如下表。

Patient: id, person_id and start_regimen_id 患者: id, person_id and start_regimen_id

Regimen: id, code 方案: id, code

Visit: id, patient_id, regimen_id,next_appointment_date 造访: id, patient_id, regimen_id,next_appointment_date

DB1 has the following tables: DB1具有下表:

Patient: id,medical_record_number,current_regimen, start_regimen nextappointment 患者: id,medical_record_number,current_regimen, start_regimen nextappointment

Now: 现在:

regimen_id data should be inserted to current_regimen schemen_id数据应插入current_regimen

start_regimen_id data should be inserted to start_regimen start_regimen_id数据应插入到start_regimen中

next_appointment_date should be inserted to nextappointment next_appointment_date应该插入到nextpointpointment

This is what I have now: 这就是我现在所拥有的:

                 SELECT
                    p.person_id AS medical_record_number,
                    r.code AS start_regimen,
                    ??? AS current_regimen,
                    DATE_FORMAT(v.next_appointment_date, '%Y-%m-%d') AS 
                    nextappointment,
                FROM patient p
                LEFT JOIN regimen r ON r.id = p.start_regimen_id
                LEFT JOIN (
                    SELECT patient_id, MAX(next_appointment_date) as next_appointment_date 
                    FROM visit 
                    WHERE next_appointment_date IS NOT NULL 
                    GROUP BY patient_id
                ) v ON v.patient_id = p.id

I have remained to migrate regimen_id (visit) on DB2 to current_regimen (patient) on DB1. 我仍然需要将DB2上的schemen_id(访问)迁移到DB1上的current_regimen(患者)。 I don't know how to use two LEFT JOIN to get data from two tables for one table. 我不知道如何使用两个LEFT JOIN从一个表的两个表中获取数据。

Any assistance will be greatly appreciated because I am really stuck. 任何帮助将不胜感激,因为我真的很困。

Seems like we would want to include regimen_id in the GROUP BY from the visit table, and then match that to id from the regimen table. 似乎我们希望将visit表中的regimen_id包括在GROUP BY中,然后将其与regimen表中的id进行匹配。 Given the outer join, it appears that patient may not have any regimen associated, so I would include matching of NULL values of regimen_id . 给定外部连接,似乎patient可能没有任何关联的regimen ,因此我将包括对regimen_idNULL值进行regimen_id

    LEFT
    JOIN ( SELECT vv.patient_id
                , vv.regimen_id
                , MAX(vv.next_appointment_date) AS next_appointment_date 
             FROM visit vv
            WHERE vv.next_appointment_date IS NOT NULL 
            GROUP
               BY vv.patient_id
                , vv.regimen_id
         ) v
      ON v.patient_id = p.id
     AND v.regimen_id <=> r.id

But that's just a guess. 但这只是一个猜测。 Without a specification (preferably illustrated by example data and and expected output) we're just guessing. 没有规范(最好由示例数据和预期输出说明),我们只是在猜测。

Note: 注意:

foo <=> bar

is a NULL-safe comparison, a shorthand equivalent to 是NULL安全比较,简写等效于

( foo = bar OR ( foo IS NULL AND bar IS NULL ) )

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

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