这是我的SQL查询:
SELECT pa.*
FROM pa
WHERE pa.id IN ( SELECT prerequis.idpa
FROM prerequis WHERE prerequis.id2a = 18)
UNION ALL
SELECT pa.*
FROM pa
WHERE pa.id IN ( SELECT prerequis.idpa
FROM prerequis WHERE prerequis.id2a = 15)
UNION ALL
SELECT pa.*
FROM pa
WHERE pa.id IN ( SELECT prerequis.idpa
FROM prerequis WHERE prerequis.id2a =32);
这给了我一个表副本中的元素列表。 现在我想根据第一个结果中的冗余情况得到相同的列表? (最常见的顶部)。
提前感谢大家的帮助:)
答案 0 :(得分:1)
首先你不需要UNION ALL
SELECT pa.*, COUNT(*)
FROM pa
INNER JOIN prerequis pr
ON pa.id = pr.idpa
WHERE pr.id2a IN (15, 18, 32)
GROUP BY pa.id
ORDER BY COUNT(*) DESC