MySQL 查询帮助

发布于 2024-12-11 13:02:42 字数 2153 浏览 3 评论 0原文

以下是我正在使用的表的描述:

describe mjla_db.StudentRecordTable2;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| classId   | varchar(20) | NO   | MUL | NULL    |       |
| studentId | varchar(20) | NO   | MUL | NULL    |       |
| quizGrade | tinyint(4)  | YES  |     | NULL    |       |
| quizId    | int(11)     | NO   | MUL | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

这是数据库中的示例数据:

+------------+-----------+------------+---------+------------+
| Student ID | Last Name | First Name | Quiz ID | Quiz Grade |
+------------+-----------+------------+---------+------------+
| A1         | Cat       | Tom        |      19 |       75   |
| A2         | pancake   | Harry      |      19 |       65   |
| A5         | Worthy    | Dick       |      19 |       NULL |
| A1         | Cat       | Tom        |      20 |       55   |
| A2         | pancake   | Harry      |      21 |       NULL |
| A2         | pancake   | Harry      |      20 |       47   |
| A5         | Worthy    | Dick       |      20 |       95   |
| A1         | Cat       | Tom        |      21 |       55   |
| A5         | Worthy    | Dick       |      21 |       95   |
+------------+-----------+------------+---------+------------+
3 rows in set (0.00 sec)

我试图获得的结果类似于以下内容:

+------------+-----------+------------+---------+------------+------------+
| Student ID | Last Name | First Name | Quiz 19 | Quiz 20    | Quiz 21    |
+------------+-----------+------------+---------+------------+------------+
| A1         | Cat       | Tom        |      75 |       55   |       55   |
| A2         | pancake   | Harry      |      65 |       47   |       NULL |
| A5         | Worthy    | Dick       |     NULL|       95   |       95   |
+------------+-----------+------------+---------+------------+------------+
  • 其中学生 ID 列是唯一的。
  • 测验列的继续位置取决于测验的数量 原来的表。测验栏包含每个的成绩 各自的学生。

Here is a description of the table i am using:

describe mjla_db.StudentRecordTable2;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| classId   | varchar(20) | NO   | MUL | NULL    |       |
| studentId | varchar(20) | NO   | MUL | NULL    |       |
| quizGrade | tinyint(4)  | YES  |     | NULL    |       |
| quizId    | int(11)     | NO   | MUL | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Here is example data in the database:

+------------+-----------+------------+---------+------------+
| Student ID | Last Name | First Name | Quiz ID | Quiz Grade |
+------------+-----------+------------+---------+------------+
| A1         | Cat       | Tom        |      19 |       75   |
| A2         | pancake   | Harry      |      19 |       65   |
| A5         | Worthy    | Dick       |      19 |       NULL |
| A1         | Cat       | Tom        |      20 |       55   |
| A2         | pancake   | Harry      |      21 |       NULL |
| A2         | pancake   | Harry      |      20 |       47   |
| A5         | Worthy    | Dick       |      20 |       95   |
| A1         | Cat       | Tom        |      21 |       55   |
| A5         | Worthy    | Dick       |      21 |       95   |
+------------+-----------+------------+---------+------------+
3 rows in set (0.00 sec)

The result i am trying to get is one that will look similar to the following:

+------------+-----------+------------+---------+------------+------------+
| Student ID | Last Name | First Name | Quiz 19 | Quiz 20    | Quiz 21    |
+------------+-----------+------------+---------+------------+------------+
| A1         | Cat       | Tom        |      75 |       55   |       55   |
| A2         | pancake   | Harry      |      65 |       47   |       NULL |
| A5         | Worthy    | Dick       |     NULL|       95   |       95   |
+------------+-----------+------------+---------+------------+------------+
  • Where the Student ID column is unique.
  • Where the quiz columns continue depending on how many quizzes are in
    the original table. And the quiz columns contain the grade of each of
    the respective students.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晨说晚安 2024-12-18 13:02:42

试试这个:

 select s.StudentId, s.FirstName, s.LastName, 
 Case when s.QuizId = 19 then quizGrade end as 'Quiz 19',
 Case when s.QuizId = 20 then quizGrade end as 'Quiz 20',
 Case when s.QuizId = 21 then quizGrade end as 'Quiz 21'
 from StudentRecordTable2 sr 
 inner join Students s on sr.StudentId = s.StudentId

参见这个

try this:

 select s.StudentId, s.FirstName, s.LastName, 
 Case when s.QuizId = 19 then quizGrade end as 'Quiz 19',
 Case when s.QuizId = 20 then quizGrade end as 'Quiz 20',
 Case when s.QuizId = 21 then quizGrade end as 'Quiz 21'
 from StudentRecordTable2 sr 
 inner join Students s on sr.StudentId = s.StudentId

see this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文