查询结构

时间:2012-02-24 01:07:41

标签: php mysql database

所以,我现在正在做一个小型的出勤系统。

我在构建必要的代码以解决我需要的问题时遇到了问题。

所以,为了简短起见,我有一个名为TAKE ATTENDANCE的页面,用户可在其中选择课程,时间开始,时间结束和小组。

我有以下表格:

CREATE TABLE period (
    periodID INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    periodStart DATE NOT NULL, -- The start date for the period
    periodEnd DATE NOT NULL, -- The end date for the period
    period_Att DATETIME, -- When the attendance should be delivered - irrelevant for now
) Engine=InnoDB;

CREATE TABLE attendance (
    attID INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,  
    att_courseID INT UNSIGNED NOT NULL,
    att_periodID INT UNSIGNED NOT NULL,
    lesson_start TIME NOT NULL,
    lesson_end TIME NOT NULL,
    att_duration TIME, --duration in time
    att_userID INT UNSIGNED NOT NULL,
    att_taken TINYINT(1) NOT NULL DEFAULT 0,
    FOREIGN KEY att_courseID REFERENCES course (courseID),
    FOREIGN KEY att_periodID REFERENCES period (periodID),
) Engine=InnoDB;

CREATE TABLE student_attendance (
    studentID INT UNSIGNED NOT NULL,
    attendanceID INT UNSIGNED NOT NULL,
    FOREIGN KEY studentID REFERENCES students (studentID),
    FOREIGN KEY attendanceID REFERENCES attendance (attendanceID),
) Engine=InnoDB;

我想要的是,在VIEW ATTENDANCE部分,用户选择课程和时间段,并按以下格式显示出勤率:

Week 1 - (30 hours of school hours per week)
1/1/2012      --- Joe Borg     4 hours (sum of hours is needed here)
              --- Nancy Ping   5 hours
              --- John Quin    5 hours
2/1/2012
3/1/2012
4/1/2012
5/1/2012
6/1/2012
7/1/2012

或者为了方便起见,我可以删除日期

Joe Borg 28 hours
Nancy Ping 30 hours
John Quin 27 hours

Week 2
etc

Week 3
etc

Week 4
etc

知道如何做SQL吗?我尝试了下面的内容虽然这得到了所有的数据并且有点令人困惑:

SELECT * FROM periods,attendance,student_attendance,students,course,stipend,users
    WHERE attendance.att_periodID = periods.periodID
    AND attendance.att_courseID = course.course_ID
    AND attendance.att_userID = users.userid
    AND student_attendance.attendanceID = attendance.attID
    AND student_attendance.studentID = students.stud_ID
    AND students.stud_StipendID = stipendID
    AND attendance.att_courseID = '$course';
    AND attendance.att_periodID = '$period';

我还根据所选课程检索了课程时间:

SELECT course_Hours FROM course
    WHERE course_ID = '$course';

获取此类数据结构需要哪些SQL?我有点困惑。

0 个答案:

没有答案