使用备用索引值获取mysql值

时间:2016-11-15 12:26:02

标签: mysql

我已经简化了mysql和给定的值。在这个master_course_id中,值应该像所需的输出一样

数据库值

master_course_id    register_number

        1               A1
        1               A2
        1               A3
        2               B1
        2               B2
        2               B3
        3               C1
        3               C2
        3               C3
        4               D1
        4               D2
        4               D3
        5               E1
        5               E2
        5               E3

当前输出

master_course_id register_number

        1            A1
        2            B1
        3            C1
        4            D1
        5            E1
        1            A2
        2            B2
        3            C2
        4            D2
        5            E2
        1            A3
        2            B3
        3            C3
        4            D3
        5            E3

所需输出

master_course_id register_number

        1             A1
        2             B1
        1             A2
        2             B2
        1             A3
        2             B3
        3             C1
        4             D1
        3             C2
        4             D2
        3             C3
        4             D3
        5             E1
        5             E2
        5             E3

Mysql查询

SELECT register_number
FROM (

        SELECT master_course_id, register_number, 
            @position := IF(master_course_id = @prev_course, @position+1, 1) AS position, 
            @prev_course := master_course_id
        FROM (SELECT * FROM master 
              WHERE master_course_id IN ('1', '2', '3', '4', '5')
              ORDER BY master_course_id, register_number) AS m,
        CROSS JOIN (SELECT @position := 0, @prev_course := null) AS vars
    ) AS t
    ORDER BY position, master_course_id

course_id 1和2应该首先如果任一个id完成意味着它应该移动到下一个id 3或4同样直到它应该循环结束。这意味着course_id可以替代如何在mysql中实现这一点

1 个答案:

答案 0 :(得分:0)

在标准SQL中,您可以使用row_number() over (partition by master_course_id order by register_number)获取register_number组中master_course_id的排名。更笨拙的是,您可以使用子查询实现相同的功能,如下面的查询所示。

其余的是符合您需求的order by条款。

select master_course_id, register_number
from
(
  select
    master_course_id,
    register_number,
    (
      select count(*)
      from master before
      where before.master_course_id = master.master_course_id
      and before.register_number < master.register_number
    ) as pos_in_course
  from master
) data
order by
  (master_course_id - 1) DIV 2, -- first id (1|2), then id (3|4), then id (5|6), ...
  pos_in_course, -- first id (1|2) pos 1, then id (1|2) pos 2, ...
  master_course_id -- first id 1 pos 1, then id 2 pos 1, then id 1 pos 2, ...

您可以对已经尝试的MySQL变量执行相同的操作。我无法检查这一点,但我认为您的查询已经正确执行了此操作,您只需要应用相应的order by子句。

最后这里是标准SQL中的相同查询:

select master_course_id, register_number
from master
order by 
  floor((master_course_id - 1) / 2),
  row_number() over (partition by master_course_id order by register_number),
  master_course_id;