从访问查询创建MySQL查询

时间:2019-06-01 23:26:27

标签: mysql ms-access

我正在尝试将Access查询转换为MySQL。我对MySQL很体面,但是不确定如何合并它。我认为来自Access的第二个查询需要成为第一个查询中MySQL的子查询,我只是不知道将它放在哪里。该查询将作为事件在MySQL服务器上运行。

将访问查询作为两个运行,第一个,它确定确定交易的交易ID。我已经转换为MySQL,并且可以正常工作了:

SELECT DISTINCT t.patient_id, MAX(t.id) AS MaxOfid
FROM transactions AS t
INNER JOIN disposition_transaction_type AS dt
    ON t.disposition_transaction_type_id = dt.id
INNER JOIN dispositions AS d
    ON dt.disposition_id = d.id 
WHERE (d.member_status = "Unenrolled" AND (t.created_at BETWEEN '2019-05-01' AND '2019-05-31')) OR ((t.created_at BETWEEN '2019-05-01' AND '2019-05-31') AND (t.disposition_transaction_type_id = 24))
GROUP BY t.patient_id;

哪个产生了正确的回报

+------------+---------+
| patient_id | MaxOfid |
+------------+---------+
|         10 |       8 |
+------------+---------+
|        112 |    4321 |
+------------+---------+

第二个查询,用于确定患者的确定性情和状况:

SELECT qryAgreedToServiceOrUnenrolled.patient_id, dispositions.description, dispositions.member_status
FROM ((qryAgreedToServiceOrUnenrolled 
INNER JOIN transactions ON qryAgreedToServiceOrUnenrolled.MaxOfid = transactions.id) 
INNER JOIN disposition_transaction_type ON transactions.disposition_transaction_type_id = disposition_transaction_type.id) 
INNER JOIN dispositions ON disposition_transaction_type.disposition_id = dispositions.id;

1 个答案:

答案 0 :(得分:1)

假设第一个要运行的查询名为qryAgreedToServiceOrUnenrolled。尝试将第二个SQL嵌套在第二个FROM子句中。可以在引用的任何地方用其他别名替换qryAgreedToServiceOrUnenrolled

SELECT qryAgreedToServiceOrUnenrolled.patient_id, dispositions.description, dispositions.member_status
FROM ((

    (SELECT DISTINCT t.patient_id, MAX(t.id) AS MaxOfid
    FROM transactions AS t
    INNER JOIN disposition_transaction_type AS dt
        ON t.disposition_transaction_type_id = dt.id
    INNER JOIN dispositions AS d
        ON dt.disposition_id = d.id 
    WHERE (d.member_status = "Unenrolled" AND (t.created_at BETWEEN '2019-05-01' AND '2019-05-31')) OR ((t.created_at BETWEEN '2019-05-01' AND '2019-05-31') AND (t.disposition_transaction_type_id = 24))
    GROUP BY t.patient_id) AS qryAgreedToServiceOrUnenrolled 

INNER JOIN transactions ON qryAgreedToServiceOrUnenrolled.MaxOfid = transactions.id) 
INNER JOIN disposition_transaction_type ON transactions.disposition_transaction_type_id = disposition_transaction_type.id) 
INNER JOIN dispositions ON disposition_transaction_type.disposition_id = dispositions.id;

我同意可能不需要DISTINCT。