如何使用 MySQL 从 3 个不同的表中输出包含所有前 5 名学生姓名的查询(也按姓名的 desc 顺序排序)?

How to use MySQL to output a query with names of all top 5 students from 3 different tables (also sort it in desc order by name)?

原题:

你的大学有三门课程。有关这些课程学生的信息存储在 mysql_students、postgresql_students 和 java_students table 中。

已经使用以下架构和所需数据创建了 table:

id          INT Primary key
name        VARCHAR(100)
score       INT

写一个 SQL 语句来查找每门课程前 5 名学生的姓名。结果 table 应该有一列 names。每门课程的学生姓名应按mysql_students、postgresql_students和java_students排序。相同课程的学生姓名按升序排列。

给出的数据:

CREATE TABLE mysql_students(
    id INT Primary key,
    name VARCHAR(100),
    score INT
);
CREATE TABLE postgresql_students(
    id INT Primary key,
    name VARCHAR(100),
    score INT
);
CREATE TABLE java_students (
    id INT Primary key,
    name VARCHAR(100),
    score INT
);

INSERT INTO  mysql_students VALUES(1,'Maria Anders',750);
INSERT INTO  mysql_students VALUES(2,'Ana Trujillo',890);
INSERT INTO  mysql_students VALUES(3,'Antonio Moreno',400);
INSERT INTO  mysql_students VALUES(4,'Thomas Hardy',910);
INSERT INTO  mysql_students VALUES(5,'Christina',600);
INSERT INTO  mysql_students VALUES(6,'Hanna',120);
INSERT INTO  mysql_students VALUES(7,'Frederique',891);
INSERT INTO  mysql_students VALUES(8,'Martin Sommer',490);
INSERT INTO  mysql_students VALUES(9,'Laurence',790);
INSERT INTO  mysql_students VALUES(10,'Elizabeth',690);
                                   
INSERT INTO  postgresql_students VALUES(1,'Victoria',750);
INSERT INTO  postgresql_students VALUES(2,'Patricio',800);
INSERT INTO  postgresql_students VALUES(3,'Francisco',400);
INSERT INTO  postgresql_students VALUES(4,'Yang',960);
INSERT INTO  postgresql_students VALUES(5,'Christina',675);
                                        
INSERT INTO  java_students VALUES(1,'Pedro',350);
INSERT INTO  java_students VALUES(2,'Elizabeth',490);
INSERT INTO  java_students VALUES(3,'Francisco',400);
INSERT INTO  java_students VALUES(4,'Sven',510);
INSERT INTO  java_students VALUES(5,'Janine',600);                                       
INSERT INTO  java_students VALUES(6,'Hanna',120);
INSERT INTO  java_students VALUES(7,'Frederique',891);

预期输出:

names
Ana Trujillo
Frederique
Laurence
Maria Anders
Thomas Hardy
Christina
Francisco
Patricio
Victoria
Yang
Elizabeth
Francisco
Frederique
Janine
Sven

请记住我是初学者。 现在,我尝试使用 union,它 almost 有效,问题是它对分数进行排序——但在获取来自 table 的记录,我不知道该怎么做。这是我目前所拥有的:

db<>fiddle here

SELECT *
FROM (
        (SELECT name 'names'
         FROM mysql_students
         ORDER BY score DESC
         LIMIT 5)
      UNION
        (SELECT name 'names'
         FROM postgresql_students
         ORDER BY score DESC
         LIMIT 5)
      UNION
        (SELECT name 'names'
         FROM java_students
         ORDER BY score DESC
         LIMIT 5))
ORDER BY NAMES

实际结果:

names
Ana Trujillo
Christina
Elizabeth
Francisco
Frederique
Janine
Laurence
Maria Anders
Patricio
Sven
Thomas Hardy
Victoria
Yang

很可能我的方法本身是错误的——我已经集思广益几个小时了,但我想不出该怎么做。请帮忙。非常感谢指导。

事实上,您的代码告诉数据库 select 数据,然后按姓名对所有第一个学生进行排序,但在作业中,他们希望您仅按姓名对前五个学生进行排序,这是您可能犯的错误尝试使用我认为有效的代码

select
  *
from 
(
  select
    *
  from 
    mysql_students
  order by score desc
  limit 5
) as t
order by name; 
select
  * 
from 
(
  select
    * 
  from 
    postgresql_students
  order by score desc 
  limit 5
) as t 
order by name; 
select
  * 
from 
(
  select
    * 
  from 
    java_students 
  order by score desc 
  limit 5
) as t
order by name; 

我找到了解决办法!!!

Select name as 'names'
from
( 
(Select name, 1 as filter from  mysql_students order by score desc limit 5)
Union all
(Select name, 2 as filter from  postgresql_students order by score desc limit 5)
union all
(Select name, 3 as filter from  java_students order by score desc limit 5)
)
as w
order by filter, names;

我查找了如何将 tables 与 UNION 结合而不更改单个 tables 的记录顺序。因此,首先我从每个 table 中选择了前 5 名学生,为每个不同 table 的每个记录分配了相同的数字。然后我联合他们。这样,即使它们在 UNION 之后变得混乱,也可以使用 filter 根据它们所属的 table 重新排序。所以在最后一行中,ORDER BY 优先 filter,然后是 name(现在称为 names)。