是否有另一种方法可以在子查询中使用LIMIT子句按类别列出第N个顶级结果?

时间:2019-01-15 00:32:40

标签: error-handling count subquery limit

在MySQL子查询中使用LIMIT关键字会产生以下错误:

此版本的MySQL尚不支持“ LIMIT&IN / ALL / ANY / SOME子查询”

此查询是否有替代方法,可以根据参与条件根据每个MemberAction类别(参与/注册/取消)中的参与计数选择前3行?

有3个表,其中包含以下列:

1)促销p

PromoType: ENUM(Form, Meeting, Survey)

PromoID: INT

2)成员m

PromoID: INT

ProspectID: INT

MemberAction: ENUM(Participate, Signup, Cancel)

3)展望pr

ProspectID INT

ProspectName VARCHAR

现在,我想根据参与人数最多的情况,为每个促销类型选择3种促销类型和前3个ProspectName。我的输出应显示PromoType和ProspectName在每个类别(调查/表单/会议)中的参与度从最高到最小(排名前3)。

SELECT p.PromoType, pr.ProspectName, 
    COUNT(m.ProspectID) AS ProspectIDCount
FROM Promo p
 JOIN Members m
    ON p.PromoID = m.PromoID
 JOIN Prospects pr
    ON m.ProspectID = pr.ProspectID
WHERE m.ProspectID IN
    (SELECT DISTINCT ProspectID
    FROM Members m
    WHERE p.PromoType = 'Survey'
        AND m.MemberAction ='Participate'
    ORDER BY p.PromoType, ProspectIDCount DESC
    LIMIT 3)
 UNION ALL
    (SELECT DISTINCT ProspectID
    FROM Members m
    WHERE p.PromoType = 'Form' 
        AND m.MemberAction ='Participate'
    ORDER BY p.PromoType, ProspectIDCount DESC
    LIMIT 3)
  UNION ALL
    (SELECT DISTINCT ProspectID 
    FROM Members m
    WHERE p.PromoType= 'Meeting'
        AND m.MemberAction ='Participate'
    ORDER BY  p.PromoType, ProspectIDCount DESC
    LIMIT 3);

预期结果:

Campaign Type                    ProspectName

Survey                          Top Participation

Survey                          2nd Most Participations

Survey                          3rd Most Participations

Form                            Top Participation

Form                            2nd Most Participations

Form                            3rd Most Participations

Meeting                         Top Participation  

Meeting                         2nd Most Participations

Meeting                         3rd Most Participations

Actual Result: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

0 个答案:

没有答案