mysql - 如何获取一个表的最小值和最大值并用它创建另一个表?

标签 mysql inner-join

我有下表。

STUDENTS
    +------+----------------+
    | stuID| StuStatus      |
    +------+----------------+
    | 1001 | 1              |
    | 1001 | 3              |
    | 1002 | 1              |
    | 1003 | 6              |
    | 1004 | 1              |
    | 1002 | 4              |
    | 1001 | 6              |
    | 1005 | 1              |
    | 1005 | 4              |
    +------+----------------+
DESCRIPTION

    +-------+--------------------------+
    | statID| StatusDesc              |
    +-------+-------------------------+
    |    1  | Application Submitted   |
    |    2  | Application Accepted    |
    |    3  | Application Pending     |
    |    4  | Application Resubmitted |
    |    5  | Application Denied      |
    =+------+-------------------------+

如何利用内连接创建一个表格,以文字形式显示每个学生的起点和终点?

这是我现在脑子里的逻辑流程:

  1. 创建两个表,均包含stuID 和stuStatus 列。 这些表格将分别显示每个学生的最低和最高学习状态。

  2. 使用内部联接创建一个新表,其中我将“DESCRIPTION”联接到表 2 和表 3。

但是,我不清楚应该如何去做,并且希望得到一些帮助。

谢谢。

最佳答案

您应该首先确定每个学生的最低和最高状态,然后将这些数字转换为相应的描述。
像这样的事情:

SELECT stu_stat.stuID,
       min_desc.statusDescr AS MinDescription,
       max_desc.statusDescr AS MaxDescription
FROM
(
SELECT StuID,
       MIN(StuStatus) AS MinStatus,
       MAX(StuStatus) AS MaxStatus
FROM   students
GROUP BY StuID
) AS stu_stat
JOIN   descr_table AS min_desc
  ON   stu_stat.MinStatus = min_desc.StatID
JOIN   descr_table AS max_desc
  ON   stu_stat.MaxStatus = max_desc.StatID;

不过我没试过...

关于mysql - 如何获取一个表的最小值和最大值并用它创建另一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54870142/

相关文章:

sql - 尝试编写一个内部连接来过滤掉一些条件

mysql - 无法从表中获取正确的图像路径

php - 在 PHP 中使用 fetchAll 返回多个数组

基于 SELECT 的 MySQL DELETE

php - 如何从同一张表中获取相关行?

php - 来自内部联接中多个列的多个 AND

php - MySQL查询打印双记录

Mysql错误1451但找不到引用id的行

mysql - 如何使用单个查询获取 mySQL 中的相对计数/频率

java - JPA 加入多列