我必须对评论次数最多的董事进行排名。根据另一个表中的行数重新排序的方法是什么?

表1(主任)

+-------------+--------------+
|    name     |    company   |
+-------------+--------------+
| John        | John SPA     |
| Mark        | Marks Food   |
| Patrick     | Patrick & CO |
+-------------+--------------+



表2(employee_reviews)

+-------------+--------------+----------+---------+
|   director  |    employee  |   Text   |  Stars  |
+-------------+--------------+----------+---------+
| John        | Omar         | ...      | 3       |
| John        | Richard      | ...      | 5       |
| Mark        | Simon        | ...      | 5       |
| Patrick     | David        | ...      | 1       |
| Patrick     | Omar         | ...      | 2       |
| John        | Simon        | ...      | 4       |
+-------------+--------------+----------+---------+





结果(导演排名):

1st) Dir. John [3 reviews]
2nd) Dir. Patrick [2 reviews]
3rd) Dir. Mark [1 review]
...

最佳答案

您需要连接到表并在作者等于导演时(打开)选择计数,然后按导演结果对行进行分组,然后对其进行排序。

示例:SELECT *, COUNT(reviews.id) FROM directors JOIN reviews ON reviews.author = directors.director GROUP BY direcotrs.director ORDER BY COUNT(reviews.id) DESC;

关于mysql - ORDER BY另一个表的行数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59702559/

10-10 15:38