我必须使用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
“;
答案 0 :(得分: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在问题中提供样本数据和表格布局会很好)