2

MySQL に次のテーブルがあります (私は php/mysql を使用しています)。

CREATE TABLE `hw_subjects` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
)  ;

...
INSERT INTO `hw_subjects` VALUES(4, 'Design');
INSERT INTO `hw_subjects` VALUES(5, 'Drama');
...
INSERT INTO `hw_subjects` VALUES(11, 'Humanities');
...
INSERT INTO `hw_subjects` VALUES(13, 'Mathematics');
...

following shows students missed homework

CREATE TABLE `hw_homeworkmissed` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `studentid` int(10) NOT NULL,
  `subjectid` int(10) NOT NULL,
  `assignment_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `teacherid` int(10) NOT NULL,
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ;


INSERT INTO `hw_homeworkmissed` VALUES(1, 29, 5, '5E', 20, '2012-10-18 13:58:40');
INSERT INTO `hw_homeworkmissed` VALUES(2, 15, 5, '32B', 20, '2012-10-18 13:59:54');
INSERT INTO `hw_homeworkmissed` VALUES(3, 29, 4, 'Q2A', 20, '2012-10-18 17:53:46');
INSERT INTO `hw_homeworkmissed` VALUES(4, 29, 11, '6E', 20, '2012-10-02 20:06:39');
INSERT INTO `hw_homeworkmissed` VALUES(5, 29, 11, 'C15', 20, '2012-10-16 20:06:30');
INSERT INTO `hw_homeworkmissed` VALUES(6, 15, 11, '7A', 20, '2012-09-19 20:08:05');
INSERT INTO `hw_homeworkmissed` VALUES(7, 29, 5, '3B', 20, '2012-09-14 20:08:12');
INSERT INTO `hw_homeworkmissed` VALUES(8, 29, 13, '6E', 32, '2012-10-18 20:23:46');
...

学生(学生ID == 29としましょう)が宿題を逃したことを表示したいと思います。だから私は科目と各科目で逃した宿題の合計が必要です. (Googleチャートを使用しています)

Mathematics,3
Science, 1
Humanities, 2 
etc.

どうすればいいですか?前もって感謝します。

アップデート:

次の配列を取得できます。しかし、これを使用できるかどうかはわかりません。Studentid = 29 の 2 つのテーブルを結合しました

Array
(
    [0] => Array
        (
            [id] => 5
            [studentid] => 29
            [subjectid] => 5
            [assignment_name] => 5E
            [teacherid] => 20
            [date] => 2012-10-18 13:58:40
            [name] => Drama
        )

    [1] => Array
        (
            [id] => 4
            [studentid] => 29
            [subjectid] => 4
            [assignment_name] => Q2A
            [teacherid] => 20
            [date] => 2012-10-18 17:53:46
            [name] => Design
        )

    [2] => Array
        (
            [id] => 11
            [studentid] => 29
            [subjectid] => 11
            [assignment_name] => 6E
            [teacherid] => 20
            [date] => 2012-10-02 20:06:39
            [name] => Humanities
        )

    [3] => Array
        (
            [id] => 11
            [studentid] => 29
            [subjectid] => 11
            [assignment_name] => C15
            [teacherid] => 20
            [date] => 2012-10-16 20:06:30
            [name] => Humanities
        )

    [4] => Array
        (
            [id] => 5
            [studentid] => 29
            [subjectid] => 5
            [assignment_name] => 3B
            [teacherid] => 20
            [date] => 2012-09-14 20:08:12
            [name] => Drama
        )

    [5] => Array
        (
            [id] => 13
            [studentid] => 29
            [subjectid] => 13
            [assignment_name] => 6E
            [teacherid] => 32
            [date] => 2012-10-18 20:23:46
            [name] => Mathematics
        )

)
....
....
4

1 に答える 1

1

SELECTおよびGROUPBYサブジェクトIDでCOUNT(id)を使用します

SELECT 
    hw_subjects.name, 
    COUNT(hw_homeworkmissed.id) 
FROM hw_subjects, hw_homeworkmissed 
WHERE hw_subjects.id= hw_homeworkmissed.subjectid 
GROUP BY subjectid 
ORDER BY hw_subjects.name
于 2012-10-18T12:57:49.633 に答える