0

我必须使用 mysql 表并尝试显示 table1 的结果,但按 table2 排序。对于 table2,我正在计算重复 id 的所有出现次数,然后按降序显示该结果。以下是我所能得到的,想知道这是否可以完成是一个单一的查询。

$query = "
SELECT DISTINCT   registration.* 

FROM              registration 

INNER JOIN        downloads 
ON                registration.id = downloads.id 

GROUP BY          downloads.COUNT(id) 
ORDER BY          downloads.COUNT(id) DESC,
                  downloads.COUNT(id) DESC

";

4

1 回答 1

0

认为您想要以下方面的内容:

SELECT Registration.id, Registration.name, Registration.email,
       Downloads.document
FROM Registration
JOIN Downloads
  ON Downloads.id = Registration.id
JOIN (SELECT id, COUNT(*) as count
      FROM Downloads
      GROUP BY id) Download_Count
  ON Download_Count.id = Registration.id
ORDER BY Download_Count.count DESC

(未经测试,因为 OP 在问题中提供示例数据和表格布局会很好)

于 2013-09-21T00:11:29.307 回答