计数多重查询

时间:2014-04-07 18:37:43

标签: mysql count inner-join

这是我用来显示流行犯罪列表的查询,以及大多数犯罪的用户。

SELECT * ,
(
    SELECT p.Username
    FROM crimes AS sc
    INNER JOIN players AS p ON p.ID = sc.crimeIssuedTo
    WHERE sc.crimeDescription = sm.crimeDescription
    GROUP BY sc.crimeIssuedTo
    ORDER BY COUNT( * ) DESC 
    LIMIT 1
) AS PersonWithMostOffenses, 

(
    SELECT p.Username
    FROM crimes AS sc
    INNER JOIN players AS p ON p.ID = sc.crimeIssuer
    WHERE sc.crimeDescription = sm.crimeDescription
    GROUP BY sc.crimeIssuer
    ORDER BY COUNT( * ) DESC 
    LIMIT 1
) AS PersonWithMostIssues




FROM crimes AS sm
GROUP BY sm.crimeDescription
ORDER BY COUNT( * ) DESC 
LIMIT 15

结果如下:

crimeID | crimeIssuer   | crimeIssuedTo | crimeDescription  | crimeIssuerName   | crimeIssuedToName | PersonWithMostOffences    | PersonWithMostIssues
2   | 59459     | 59464     | Grand Theft       | Cop_Name      | Criminal_Name     | Criminal_Name         | CopName

但我也想添加MostOffencesCount和MostIssuesCount。如何将其添加到此查询中?

0 个答案:

没有答案