0

我想要做的是从表中拉出id, phone_type,os_versionEnswitch_Mobile_Users

有了 id,我只需enswitch_idEnswitch_Users表中获取。

之后,对来自Enswitch_Android_PurchasesEnswitch_Iphone_Purchases用户列与来自的 id 匹配的所有整体进行计数enswitch_mobile_users。并获取第一次进入日期和最后一次进入日期。

我设法使它与这个查询一起工作:

SELECT p.user  AS `Mobile_User_ID`, 
    e.os_version `Os_Version`, 
    e.phone_type `Phone_Type`, 
    eu.enswitch_id `Enswitch_ID`,
    Count(1) AS `Buy_Count`,
    (SELECT pc.date 
        FROM 
        (
            SELECT date, user, status 
            FROM enswitch_android_purchases 
            UNION 
            SELECT date, user, status 
            FROM enswitch_iphone_purchases
        ) AS pc 
        WHERE pc.status = 1 
            AND pc.user = p.user 
        ORDER BY pc.date ASC 
        LIMIT 1) AS `First_Purchase`,
    (SELECT pc.date 
        FROM 
        (
            SELECT date, user, status 
            FROM enswitch_android_purchases 
            UNION 
            SELECT date, user, status 
            FROM enswitch_iphone_purchases
        ) AS pc 
        WHERE pc.status = 1 
            AND pc.user = p.user 
        ORDER BY pc.date DESC LIMIT 1) AS `Last_Purchase`
FROM 
(
    SELECT item, date, user, status 
    FROM enswitch_android_purchases 
    UNION
    SELECT item, date, user, status 
    FROM enswitch_iphone_purchases
) AS p 
LEFT JOIN enswitch_mobile_users e 
    ON p.user = e.id
LEFT JOIN enswitch_users eu 
    ON e.user_id = eu.id
WHERE p.`date` >= :from_date 
    AND p.`date` <= :to_date 
    AND p.user is not null 
    AND p.status = 1
GROUP BY `Mobile_User_ID`

但是由于选择它会很慢所以我怎样才能让它更有效率呢?

4

1 回答 1

1

您也许可以使用以下内容将SELECT列表中的两个选择替换为min(p.date)and max(p.date)

SELECT p.user  AS `Mobile_User_ID`, 
    e.os_version `Os_Version`, 
    e.phone_type `Phone_Type`, 
    eu.enswitch_id `Enswitch_ID`,
    Count(1) AS `Buy_Count`,
    min(p.date) AS `First_Purchase`,
    max(p.date) AS `Last_Purchase`
FROM 
(
    SELECT item, date, user, status 
    FROM enswitch_android_purchases 
    UNION
    SELECT item, date, user, status 
    FROM enswitch_iphone_purchases
) AS p 
LEFT JOIN enswitch_mobile_users e 
    ON p.user = e.id
LEFT JOIN enswitch_users eu 
    ON e.user_id = eu.id
WHERE p.`date` >= :from_date 
    AND p.`date` <= :to_date 
    AND p.user is not null 
    AND p.status = 1
GROUP BY p.user
于 2013-01-07T15:56:09.100 回答