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

时间:2012-09-25 10:57:27

标签: php mysql

我在下面有三张表格,显示学生的记录,科目和有学科的学生。

我想问一下有效的SQL查询是什么,以显示下面的结果。我可以使用JOIN显示它,但不能使用下面的格式显示它。

+------+-----------+-----------+-----+----------+-----------+--------+
| 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 个答案:

答案 0 :(得分:1)

怎么样

选择a.name为" Name",a.address as" Address",a.telephone as" Telephone" ,a.sex为" Sex",null为" Subject",a.teacher为" Teacher",a.active为" Active"来自student_record作为 将a.name命名为" 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)内部联接主题为b on c.student_id_subject = b.id_subject

未测试过。它与您的示例的顺序相同,但应该包含所有数据