在 MySql 中执行此操作的一种方法
SELECT c.class_name, r.room_name
FROM
(
SELECT class_name, @n := @n + 1 rnum
FROM classes CROSS JOIN (SELECT @n := 0) i
ORDER BY class_name
) c JOIN
(
SELECT room_name, @m := @m + 1 rnum
FROM classrooms CROSS JOIN (SELECT @m := 0) i
ORDER BY room_name
) r
ON c.rnum = r.rnum
输出:
| CLASS_NAME | 房间名称 |
-------------|------------|
| 微积分 | 101 室 |
| 英语 | 102 室 |
| 历史 | 201 室 |
这是SQLFiddle演示
Postgres 中的相同内容看起来像
SELECT c.class_name, r.room_name
FROM
(
SELECT class_name,
ROW_NUMBER() OVER (ORDER BY class_name) rnum
FROM classes
) c JOIN
(
SELECT room_name,
ROW_NUMBER() OVER (ORDER BY room_name) rnum
FROM classrooms
) r
ON c.rnum = r.rnum
这是SQLFiddle演示
在 SQLite 中
SELECT c.class_name, r.room_name
FROM
(
SELECT class_name,
(SELECT COUNT(*)
FROM classes
WHERE c.class_name >= class_name) rnum
FROM classes c
) c JOIN
(
SELECT room_name,
(SELECT COUNT(*)
FROM classrooms
WHERE r.room_name >= room_name) rnum
FROM classrooms r
) r
ON c.rnum = r.rnum
这是SQLFiddle演示