使用直接匹配对足球/足球排行榜表进行排序

3xiyfsfu  于 2021-08-09  发布在  Java
关注(0)|答案(0)|浏览(108)

我要为我的项目找一个分类领导。我需要帮助,因为我必须按积分、进球差距和直接比赛来排序。我的问题是直接匹配。mysql版本:5.7.29
以下是表格结构和一些示例数据:

CREATE TABLE `matches` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `club_id_home` bigint(20) UNSIGNED NOT NULL,
  `goals_home` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
  `club_id_away` bigint(20) UNSIGNED NOT NULL,
  `goals_away` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
  `competition_id` bigint(20) UNSIGNED DEFAULT NULL,
  `round` smallint(5) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `matches` (`id`, `club_id_home`, `goals_home`, `club_id_away`, `goals_away`, `competition_id`, `round`) VALUES
(1, 1, 3, 2, 2, 1, 1),
(2, 3, 0, 4, 0, 1, 1),
(3, 3, 1, 1, 1, 1, 1),
(4, 4, 1, 2, 0, 1, 1);

CREATE TABLE `leaderboards` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `club_id` bigint(20) UNSIGNED NOT NULL,
  `competition_id` bigint(20) UNSIGNED DEFAULT NULL,
  `points` tinyint(3) UNSIGNED NOT NULL,
  `wins` tinyint(3) UNSIGNED NOT NULL,
  `losses` tinyint(3) UNSIGNED NOT NULL,
  `draws` tinyint(3) UNSIGNED NOT NULL,
  `goals_for` tinyint(3) UNSIGNED NOT NULL,
  `goals_against` tinyint(3) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `leaderboards` (`id`, `club_id`, `competition_id`, `points`, `wins`, `losses`, `draws`, `goals_for`, `goals_against`) VALUES
(1, 1, 1, 4, 1, 1, 0, 4, 3),
(2, 2, 1, 0, 0, 0, 2, 2, 4),
(3, 3, 1, 2, 0, 2, 0, 1, 1),
(4, 4, 1, 4, 1, 1, 0, 1, 0);

因此,我必须检查积分(容易)和目标差异(容易)从排行榜表,并直接从比赛表的比赛,所以如果俱乐部a和俱乐部b有相同的积分,我必须检查俱乐部a赢了或输了(或并列)对俱乐部b。
所以在我的例子中,我想看到一个排行榜排序如下:

club_id 4 | points: 4 | goals difference: 1
club_id 1 | points: 4 | goals difference: 1
club_id 3 | points: 2 | goals difference: 0
club_id 2 | points: 0 | goals difference: -2

第四俱乐部是第一个,因为它赢得了与第一俱乐部的直接比赛。我该怎么做?有可能吗?
提前谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题