如何在查询中计算两个不同的东西

时间:2014-04-07 17:12:50

标签: php mysql count

我收到了这个问题:

$q = $mysql->query("
SELECT *, COUNT(`crimeDescription`) AS total_count 
FROM `crimes` 
GROUP BY `crimeDescription` 
ORDER BY COUNT(`crimeDescription`) DESC 
Limit 15") or die($mysql->error);

我也想让那些曾经犯过这种罪行的人多次遭到反对(犯罪问题)

示例:

1 | Grand Theft Auto | Most to: User_A | Total: 500
2 | Attempted Murder | Most to: User_C | Total: 453
3 | Assault          | Most to: User_D | Total: 451

我该怎么做?

1 个答案:

答案 0 :(得分:0)

如下所示:

SELECT *, COUNT(*) AS total_count, 
(
  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
FROM crimes AS sm
GROUP BY sm.crimeDescription
ORDER BY COUNT(*) DESC 
LIMIT 15