简体   繁体   English

每个具有组内存的组的MySQL行号

[英]MySQL row number for each group with group memory

I'm doing a running program in Java and MySQL. 我正在用Java和MySQL做一个正在运行的程序。 So the runners can have statistic of who is in first place after the races finish. 因此,跑步者可以掌握比赛结束后谁在第一位的统计数据。 Also runners races under a category. 赛跑者也在一个类别下比赛。 So also there are winner under every category. 因此,每个类别下都有赢家。 All the data are in separate tables in MySQL, and when the races end, a query must show table position general and by category, depending of how quick they finish. 所有数据都存储在MySQL的单独表中,当竞赛结束时,查询必须显示表的常规位置和类别,具体取决于它们完成的速度。

I have those tables in 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)

And I have this query that give me the table position once the race is finished. 我有这个查询,一旦比赛结束,它就会给我表格位置。

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;

The reason of this first query is because I need the information of the table position. 第一个查询的原因是因为我需要表位置的信息。 I include "race.end IS NULL" in group clause because I need runners without end time (didn't finish the race) at the end of the table. 我在组子句中包括“ race.end IS NULL”,因为我需要表尾没有结束时间(没有完成比赛)的跑步者。

This give me some result like this: 这给了我这样的结果:

在此处输入图片说明

OK. 好。 If I try to do the normal procedure of adding "row number" everything got a mess because, the query first assign number to the row, and then apply order clause in it. 如果我尝试执行添加“行号”的常规过程,那么一切都会一团糟,因为查询首先将数字分配给该行,然后在其中应用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;

And mess is everywhere: 混乱无处不在:

在此处输入图片说明

OK. 好。 After been a while in search I do a query to get my "table position", and then cross it with another one to put row number so far. 经过一段时间的搜索后,我进行查询以获取“表格位置”,然后将其与另一个表格交叉以显示到目前为止的行号。 Works fine. 工作正常。

Now the category problem. 现在的类别问题。

When try to do the same thing with the category system, the query only count consecutive sequence of the number, there is no memory in the category like "10 rows back where another master at 4 position, so this need to be 5". 当尝试对类别系统执行相同的操作时,查询仅计算数字的连续序列,类别中没有诸如“向后10行,其中另一个母版位于4位置,因此该值为5”之类的内存。 For be more specific attach a code of what is wrong and what should be. 具体来说,附上错误代码和错误代码。

WRONG: 错误:

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

Is wrong because if you see at position 3, there is a "Master" with the first place, and then a position 6 another "Master" with the same position, and the same at 10 position. 这是错误的,因为如果您在位置3处看到,则第一个位置有一个“ Master”,然后在位置6处有另一个“ Master”,其位置相同,而第10个位置相同。

GOOD: 好:

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

And so on. 等等。 Always there is different category name, so a category list is not a proper solution. 总是有不同的类别名称,因此类别列表不是正确的解决方案。

The final query used for this result is: 用于此结果的最终查询是:

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;

Never mind, I just find how to do it... 没关系,我只是找到方法...

It something related to how MySQL understand the query, and how process the query for results... 它与MySQL如何理解查询以及如何处理查询结果有关...

Jus play a little bit with the ORDER clause and add a new level into query z. Jus在ORDER子句中扮演了一点角色,并将新级别添加到查询z中。 , y. ,y。 and x.* 和x。*

z.* for the row number y.* for the "category" position (order by category) x.* the original query. z。*表示行号y。*表示“类别”位置(按类别排序)x。*原始查询。

Thanks anyway. 不管怎么说,还是要谢谢你。

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

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