1

给定一个 *students_exam_rooms* 表:

+------------+---------+---------+
| student_id | room_id | seat_no |
+------------+---------+---------+
|          1 |      30 |    1001 |
|          2 |      30 |    1002 |
|          3 |      31 |    2001 |
|          4 |      32 |    2002 |
|          5 |      33 |    3001 |
|          6 |      33 |    3002 |
|          7 |      34 |    4001 |
|          8 |      34 |    4002 |
+------------+---------+---------+

和 *students_tbl*:

+------------+-------------+------+
| student_id | studen_name | year |
+------------+-------------+------+
|          1 | Eric        |    1 |
|          2 | Mustafa     |    1 |
|          3 | Michael     |    2 |
|          4 | Andy        |    2 |
|          5 | Rafael      |    3 |
|          6 | Mark        |    3 |
|          7 | Jack        |    4 |
|          8 | peter       |    4 |
+------------+-------------+------+

如何从 *students_tbl.year* 订购的 *students_exam_rooms* 中进行选择,但像这样一个接一个:

+--------------+------+
| student_name | year |
+--------------+------+
| Eric         |    1 |
| Michael      |    2 |
| Rafael       |    3 |
| Jack         |    4 |
| Mustafa      |    1 |
| Andy         |    2 |
| Mark         |    3 |
| Peter        |    4 |
+--------------+------+
4

3 回答 3

2

我假设您想按年份的“出现次数”排序,然后按年份排序,例如所有年份的所有第一次出现,按年份排序,然后所有年份的所有第二次出现也按年份排序, 等等。这将是模拟其他 RDBMS 的分析/窗口函数的完美案例:

select *
from (
    select 
        s.studen_name,
        s.year,
        ser.*,
        (
            select 1 + count(*)
            from students_tbl s2
            where s.year = s2.year
                and s.student_id > s2.student_id
        ) rank
    from students_tbl s
        JOIN students_exam_rooms ser
            ON s.student_id = ser.student_id
) i_dont_really_want_to_name_this
order by rank, year

这是针对 JW 小提琴的略微调整版本:http ://www.sqlfiddle.com/#!2/27c91/1

Emulating Analytic (AKA Ranking) Functions with MySQL是一篇很好的文章,提供了更多背景和解释。

于 2013-03-27T14:41:01.907 回答
1

尝试以下任何一项:

SELECT  a.studen_name, a.year
FROM    students_tbl a
        INNER JOIN students_exam_rooms b
          ON a.student_id = b.student_id
ORDER   BY  REVERSE(b.seat_no),
            a.year

通过使用Modulo

SELECT  a.studen_name, a.year
FROM    students_tbl a
        INNER JOIN students_exam_rooms b
          ON a.student_id = b.student_id
ORDER   BY CASE WHEN MOD(b.seat_no, 2) <> 0 THEN 0 ELSE 1 END,
            a.year
于 2013-03-27T12:18:25.690 回答
0

在我看来,您尝试先按座位排序,然后按年份排序。看着你的students_exam_rooms桌子,看起来你从一个简单的座位号开始,并在前面加上year * 1000. 所以,如果我们省略年份,它看起来像这样:

> select * from fixed_students_exam_rooms;
+------------+---------+---------+
| student_id | room_id | seat_no |
+------------+---------+---------+
|          1 |      30 |       1 |
|          2 |      30 |       2 |
|          3 |      31 |       1 |
|          4 |      32 |       2 |
|          5 |      33 |       1 |
|          6 |      33 |       2 |
|          7 |      34 |       1 |
|          8 |      34 |       2 |
+------------+---------+---------+

如果你有那个表,你的查询很简单:

select
  student_name, year

from
  modified_student_exame_rooms
  left join students_tbl using (student_id)

order by
  seat_no, year
;

使用您当前拥有的表格,假设“核心座位号”不超过 999,它只会稍微复杂一些。

select
  student_name, year

from
  modified_student_exame_rooms
  left join students_tbl using (student_id)

order by
  convert(substr(seat_no, 2), unsigned),
  year
;
于 2013-03-27T15:02:18.520 回答