dqyl2374 2012-09-25 10:57
浏览 29
已采纳

MySQL查询显示基于三个表的格式

I have three tables below that shows the student records, subjects and students with subjects.

I would like to ask what is the effective SQL query to show the results below. I can show it using JOIN but not with the format below.

+------+-----------+-----------+-----+----------+-----------+--------+
| Name | Address   | Telephone | Sex | Subjects | Teacher   | Active |
+------+-----------+-----------+-----+----------+-----------+--------+
| John | somewhere | 12345     | M   |          | Teacher 1 | YES    |
| John | somewhere | 12345     | M   | Math     |           | YES    |
| John | somewhere | 12345     | M   | Science  |           | YES    |
| John | somewhere | 12345     | M   | English  |           | YES    |
| Matt | somewhere | 123456    | M   |          | Teacher 2 | YES    |
| Matt | somewhere | 23456     | M   | Math     |           | YES    |
| Matt | somewhere | 123456    | M   | Science  |           | YES    |
| Girl | somewhere | 5431      | F   |          | Teacher3  | YES    |
| Girl | somewhere | 5431      | F   | Physics  |           | YES    |
| Girl | somewhere | 5431      | F   | Math     |           | YES    |
+------+-----------+-----------+-----+----------+-----------+--------+

select * from student_record;
+------------+------+-----------------+-----------+-----+----------+--------+
| id_student | name | address         | telephone | sex | teacher  | active |
+------------+------+-----------------+-----------+-----+----------+--------+
|          1 | John | Somewhere       | 12345     | M   | Teacher  | 0      |
|          2 | Matt | Somewhere There | 12345222  | M   | Teacher1 | 0      |
|          3 | Girl | Somewhere here  | 3333      | F   | Teacher2 | 0      |
+------------+------+-----------------+-----------+-----+----------+--------+

select * from subjects;
+------------+--------------+---------------------+
| id_subject | subject_name | subject_description |
+------------+--------------+---------------------+
|          1 | Math         | Math                |
|          2 | Science      | Science             |
|          3 | English      | English             |
|          4 | Physics      | Physics             |
+------------+--------------+---------------------+

select * from with_subjects;
+--------------------+--------------------+------------+
| id_student_subject | student_id_subject | student_id |
+--------------------+--------------------+------------+
|                  1 |                  1 |          1 |
|                  2 |                  2 |          1 |
|                  3 |                  3 |          1 |
|                  4 |                  4 |          1 |
|                  5 |                  4 |          2 |
|                  6 |                  3 |          2 |
|                  8 |                  1 |          2 |
|                  9 |                  1 |          3 |
|                 10 |                  2 |          3 |
|                 11 |                  3 |          3 |
|                 12 |                  4 |          3 |
+--------------------+--------------------+------------+
  • 写回答

1条回答 默认 最新

  • douyan1927 2012-09-25 11:30
    关注

    how about

    select a.name as "Name",a.address as "Address",a.telephone as "Telephone" ,a.sex as "Sex",null as "Subject",a.teacher as "Teacher",a.active as "Active" from student_record as a union a.name as "Name",a.address as "Address",a.telephone as "Telephone" ,a.sex as "Sex",b.subject_name as "Subject",null as "Teacher",a.active as "Active" from (student_record as a inner join with_subjects as c on a.id_student = c.student_id) inner join subjects as b on c.student_id_subject = b.id_subject

    Not tested it. It wotn be in the same order as your example, but should have all of the data there

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大