具有组内存

时间:2016-10-19 02:48:35

标签: java mysql

我正在用Java和MySQL做一个正在运行的程序。所以跑步者可以在比赛结束后统计谁在第一名。另外一个类别的跑步者比赛。所以每个类别下都有胜利者。所有数据都在MySQL中的单独表中,当比赛结束时,查询必须显示表位置一般和类别,具体取决于它们完成的速度。

我在MySQL中有这些表:

events (id, name, isActive)
runners (id, name, sex)
category (id, name, isActive)
inscriptions (id, event_id, runner_id, category, number_assigned, isActive)
race (id, inscription_id, start, end)

我有这个查询,一旦比赛结束就给我表格位置。

SELECT  category.`name` AS `CATEGORÍA`, inscriptions.number_assigned AS `NÚMERO`, runners.name AS `COMPETIDOR`, runners.sex AS `SEXO`, races.`start` AS `INICIO`, races.`end` AS `FIN`, SEC_TO_TIME(UNIX_TIMESTAMP(races.`end`) - UNIX_TIMESTAMP(races.`start`)) AS `TIEMPO`
    FROM `events` 
        INNER JOIN `inscriptions` ON inscriptions.event_id = `events`.id 
        INNER JOIN `races` ON races.inscription_id = inscriptions.id 
        INNER JOIN `category` ON inscriptions.category = category.id 
        INNER JOIN `runners` ON inscriptions.runner_id = runners.id
    WHERE `events`.id = 1 
        AND inscriptions.isFinished = 0 
        AND `events`.isActive = 1 
        AND `inscriptions`.isActive = 1 
        AND `category`.isActive = 1 
        AND `runners`.isActive = 1
    ORDER BY races.`end` IS NULL, SEC_TO_TIME(UNIX_TIMESTAMP(races.`end`) - UNIX_TIMESTAMP(races.`start`)) ASC;

第一次查询的原因是因为我需要表位置的信息。我在组子句中包含“race.end IS NULL”,因为我需要在表的末尾没有结束时间(没有完成比赛)的跑步者。

这给我一些结果:

enter image description here

行。如果我尝试执行添加“行号”的正常过程,一切都搞得一团糟,因为查询首先为行分配编号,然后在其中应用order子句。

SELECT 
    @r := @r+1 AS `POSICION`,
    `category`.`name` AS `CATEGORÍA`,
    `inscriptions`.`number_assigned` AS `NÚMERO`,
    `runner`.`name` AS `COMPETIDOR`,
    `runners`.`sex` AS `SEXO`,
    `races`.`start` AS `INICIO`,
    `races`.`end` AS `FIN`,
    SEC_TO_TIME(UNIX_TIMESTAMP(`races`.`end`) - UNIX_TIMESTAMP(`races`.`start`)) AS `TIEMPO`
  FROM
    (SELECT @r:=0)y, `events`
    INNER JOIN `inscriptions` ON (`inscriptions`.`event_id` = `events`.`id`)
    INNER JOIN `races` ON (`races`.`inscription_id` = `inscriptions`.`id`)
    INNER JOIN `category` ON (`inscriptions`.`category` = `category`.`id`)
    INNER JOIN `runners` ON (`inscriptions`.`runner_id` = `runners`.`id`)
  WHERE
    `events`.`id` = 1 AND 
    `inscriptions`.`isFinished` = 0 AND 
    `events`.`isActive` = 1 AND 
    `inscriptions`.`isActive` = 1 AND 
    `category`.`isActive` = 1 AND 
    `runners`.`isActive` = 1 AND 
    `races`.`end` IS NOT NULL
  ORDER BY 
    `races`.`end` IS NULL, 
    SEC_TO_TIME(UNIX_TIMESTAMP(`races`.`end`) - UNIX_TIMESTAMP(`races`.`start`)) ASC;

到处乱七八糟:

enter image description here

行。经过一段时间的搜索后,我做了一个查询来获取我的“表位”,然后将其与另一个交叉以放入行号到目前为止。工作正常。

现在是类别问题。

当尝试对类别系统做同样的事情时,查询只计算连续的数字序列,类别中没有内存,如“10行返回另一个主人在4位置,所以这需要是5 ”。更具体地说,附上一个错误和应该是什么的代码。

错误:

1   1   Especiales  Especiales  614 Alex Chancusi   M   2016-10-09 07:12:53 2016-10-09 07:32:16 00:19:23
2   1   Juvenil Juvenil 491 Anthony Recalde Carrillo    M   2016-10-09 07:12:53 2016-10-09 07:35:34 00:22:41
3   1   Master  Master  610 Marco Almache   M   2016-10-09 07:12:53 2016-10-09 07:35:50 00:22:57
4   1   Senior  Senior  632 Cristian Rafael Caizapanta  M   2016-10-09 07:12:53 2016-10-09 07:36:17 00:23:24
5   2   Senior  Senior  138 Dennys Morocho Guayanlema   M   2016-10-09 07:12:53 2016-10-09 07:37:00 00:24:07
6   1   Master  Master  591 Manuel Suntaxi  M   2016-10-09 07:12:53 2016-10-09 07:37:35 00:24:42
7   1   Senior  Senior  508 Jhon Robles M   2016-10-09 07:12:53 2016-10-09 07:38:44 00:25:51
8   2   Senior  Senior  536 Margaret Karic Zoroitich    F   2016-10-09 07:12:53 2016-10-09 07:38:53 00:26:00
9   3   Senior  Senior  538 Carlos Moreno   M   2016-10-09 07:12:53 2016-10-09 07:39:20 00:26:27
10  1   Master  Master  550 Luis Toaquiza   M   2016-10-09 07:12:53 2016-10-09 07:39:43 00:26:50

错了,因为如果你在第3位看到第一个位置有一个“主人”,那么位置6是另一个“主人”,位置相同,并且位于10位。

好的:

1   1   Especiales  Especiales  614 Alex Chancusi   M   2016-10-09 07:12:53 2016-10-09 07:32:16 00:19:23
2   1   Juvenil Juvenil 491 Anthony Recalde Carrillo    M   2016-10-09 07:12:53 2016-10-09 07:35:34 00:22:41
3   1   Master  Master  610 Marco Almache   M   2016-10-09 07:12:53 2016-10-09 07:35:50 00:22:57
4   1   Senior  Senior  632 Cristian Rafael Caizapanta  M   2016-10-09 07:12:53 2016-10-09 07:36:17 00:23:24
5   2   Senior  Senior  138 Dennys Morocho Guayanlema   M   2016-10-09 07:12:53 2016-10-09 07:37:00 00:24:07
6   2   Master  Master  591 Manuel Suntaxi  M   2016-10-09 07:12:53 2016-10-09 07:37:35 00:24:42
7   3   Senior  Senior  508 Jhon Robles M   2016-10-09 07:12:53 2016-10-09 07:38:44 00:25:51
8   4   Senior  Senior  536 Margaret Karic Zoroitich    F   2016-10-09 07:12:53 2016-10-09 07:38:53 00:26:00
9   5   Senior  Senior  538 Carlos Moreno   M   2016-10-09 07:12:53 2016-10-09 07:39:20 00:26:27
10  3   Master  Master  550 Luis Toaquiza   M   2016-10-09 07:12:53 2016-10-09 07:39:43 00:26:50

等等。始终存在不同的类别名称,因此类别列表不是正确的解决方案。

用于此结果的最终查询是:

SELECT @posGeneral := CASE WHEN z.`FIN` IS NULL THEN 'Not finished yet' ELSE @posGeneral+1 END AS `POS GENERAL`, 
  @posCat := CASE WHEN z.`FIN` IS NULL THEN '-' ELSE CASE WHEN @cat = z.`CATEGORÍA` THEN @posCat + 1 ELSE 1 END END AS `POS CATEGORÍA`, 
  @cat := z.`CATEGORÍA` AS `CAT`,
  z.* FROM(
  SELECT 
    `category`.`name` AS `CATEGORÍA`,
    `inscriptions`.`number_assigned` AS `NÚMERO`,
    `runner`.`name` AS `COMPETIDOR`,
    `runners`.`sex` AS `SEXO`,
    `races`.`start` AS `INICIO`,
    `races`.`end` AS `FIN`,
    SEC_TO_TIME(UNIX_TIMESTAMP(`races`.`end`) - UNIX_TIMESTAMP(`races`.`start`)) AS `TIEMPO`
  FROM
    `events`
    INNER JOIN `inscriptions` ON (`inscriptions`.`event_id` = `events`.`id`)
    INNER JOIN `races` ON (`races`.`inscription_id` = `inscriptions`.`id`)
    INNER JOIN `category` ON (`inscriptions`.`category` = `category`.`id`)
    INNER JOIN `runners` ON (`inscriptions`.`runner_id` = `runners`.`id`)
  WHERE
    `events`.`id` = 1 AND 
    `inscriptions`.`isFinished` = 1 AND 
    `events`.`isActive` = 1 AND 
    `inscriptions`.`isActive` = 1 AND 
    `category`.`isActive` = 1 AND 
    `runners`.`isActive` = 1
  ORDER BY 
    `races`.`end` IS NULL, 
    SEC_TO_TIME(UNIX_TIMESTAMP(`races`.`end`) - UNIX_TIMESTAMP(`races`.`start`)) ASC
)z, (SELECT @posGeneral:=0, @posCat:=0)y;

1 个答案:

答案 0 :(得分:0)

没关系,我只是想知道怎么做......

它与MySQL如何理解查询有关,以及如何处理查询结果......

Jus使用ORDER子句稍微玩一下,并在查询z中添加一个新级别。,y。和x。*

z。*表示行号 y。*表示“类别”的位置(按类别排序) x。*原始查询。

非常感谢。