简体   繁体   English

如何使三列按目标数排序?

[英]How do I make three columns combine ordered by the number of goals?

I'm having some problems with unifying two tables in SQL.我在统一 SQL 中的两个表时遇到了一些问题。 I want to get the players that scored more goals in the season, but I cannot get it right.我想得到本赛季进球更多的球员,但我做不到。

I've done both things separate ways:我已经以不同的方式完成了这两件事:

SELECT name, surname
FROM players
GROUP BY name, surname

If I do this I get all the names from the players, but if I mix it with the other on a JOIN ON some players disappear with no apparent reason.如果我这样做,我会从玩家那里得到所有的名字,但是如果我在 JOIN ON 上将它与另一个混在一起,一些玩家会无缘无故地消失。

RESULT:结果:

| name    | surname   |

| John    | Brown     |
| Robert  | Smith     |
| Albert  | Carpenter |
| David   | Addams    |
| Richard | McAvoy    |

Then I have the number of goals that each one scored, I get this by doing a COUNT(*)然后我有每个人的进球数,我通过 COUNT(*) 得到这个

SELECT COUNT(*) AS goals
FROM goals
JOIN players ON (players.id = goals.player_id)
GROUP BY player_id
ORDER BY goals DESC
LIMIT 5;

Down in this bit I can get the desired results as well, which are the 5 highest scores in goals.在这一点上,我也可以获得预期的结果,即进球得分最高的 5 个。 But I don't know how to get a combined result.但我不知道如何获得综合结果。

RESULT:结果:

| goals |

| 152   |
| 140   |
| 102   |
| 95    |
| 91    |

I want my columns to be:我希望我的专栏是:

| Name    | Surname   | goals |

| John    | Brown     | 152   |
| Robert  | Smith     | 140   |
| Albert  | Carpenter | 102   |
| David   | Addams    | 95    |
| Richard | McAvoy    | 91    |

Any help is appreciated.任何帮助表示赞赏。 :) :)

Try this尝试这个

SELECT p.name, p.surname, g.goals
FROM goals g
INNER JOIN players p ON p.id = g.player_id
ORDER BY g.goals DESC LIMIT 5;

Better if you show some records from both table, then it will be easier to answer.如果您显示两个表中的一些记录会更好,那么它会更容易回答。 Now I'm assuming your goal table is like this playerid -> no.现在我假设你的目标表就像这个 playerid -> 不。 of goals目标

SELECT name, surname, COUNT(*) AS goals SELECT name, surname, COUNT(*) AS 目标

FROM goals从目标

inner JOIN players ON (players.id = goals.player_id)内部加入玩家开启(players.id = goal.player_id)

GROUP name, surname组名,姓氏

ORDER BY goals DESC按目标排序 DESC

LIMIT 5;限制 5;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM