您好我正在为小学开发一个系统而且我在数据库部分遇到了查询问题。我想找回:
我不是很擅长查询,我无法解决问题。我感谢任何帮助。感谢
您可以在下面找到我的表格和测试数据
表:
CREATE TABLE Student
(
StudentID INT auto_increment,
ClassID VARCHAR(10),
FirstName VARCHAR(25),
LastName VARCHAR(25),
DateOfBirth DATE,
Gender VARCHAR(10),
PRIMARY KEY (StudentID, ClassID)
);
create table Subjects
(
SubjectID INT,
SubjectName VARCHAR(25),
PRIMARY KEY (SubjectID)
);
create table Grade
(
StudentID INT,
SubjectID INT,
ClassID VARCHAR(10),
Grade DECIMAL(5,1),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (SubjectID) REFERENCES Subjects(SubjectID),
FOREIGN KEY (ClassID) REFERENCES Student(ClassID)
);
测试数据:
INSERT INTO Student (StudentID, ClassID, FirstName, LastName, DateOfBirth, Gender)
VALUES ('', '01', 'John', 'Smith', '15/01/2000', 'Male'),
('', '01', 'Michael', 'Black', '15/03/2000', 'Male'),
('', '01', 'Dennis', 'White', '07/08/1999', 'Male'),
('', '01', 'Emy', 'Blue', '10/04/2000', 'Female'),
('', '01', 'Joe', 'Yellow', '09/05/2000', 'Male'),
('', '01', 'Aaren', 'Jackson', '09/009/1999', 'Male'),
('', '01', 'Marta', 'Harris', '30/01/2000', 'Female'),
('', '01', 'Laura', 'Lewis', '09/01/2000', 'Female'),
('', '01', 'Michael', 'Jackson', '01/01/2000', 'Male'),
('', '02', 'Piotr', 'Kowalski', '15/01/2000', 'Male'),
('', '02', 'Harris', 'Bialy', '15/03/2000', 'Male'),
('', '02', 'Porter', 'Czarny', '07/08/1999', 'Male'),
('', '02', 'Maciek', 'Blue', '10/04/2000', 'Female'),
('', '02', 'Mateusz', 'Yellow', '09/05/2000', 'Male'),
('', '02', 'Aaren', 'Jackson', '09/009/1999', 'Male'),
('', '02', 'Marta', 'Harris', '30/01/2000', 'Female'),
('', '02', 'Laura', 'Lewis', '09/01/2000', 'Female'),
('', '02', 'Chaytan', 'Jackson', '01/01/2000', 'Male');
INSERT INTO Subjects (SubjectID, SubjectName)
VALUES ('01', 'English'),
('02', 'Mathematics'),
('03', 'Science'),
('04', 'Geography'),
('05', 'IT'),
('06', 'History');
INSERT INTO Grade (StudentID, SubjectID, ClassID, Grade)
VALUES ('1', '2', '01', '60.5'),
('2', '2', '01', '70.0'),
('3', '2', '01', '40.0'),
('4', '2', '01', '33.5'),
('5', '2', '01', '90.0'),
('6', '2', '01', '77.5'),
('7', '2', '01', '89.0'),
('8', '2', '01', '74.0'),
('9', '2', '01', '66.5'),
('10', '2', '02', '30.5'),
('11', '2', '02', '50.0'),
('12', '2', '02', '30.0'),
('13', '2', '02', '73.5'),
('14', '2', '02', '70.0'),
('15', '2', '02', '57.5'),
('16', '2', '02', '69.0'),
('17', '2', '02', '34.0'),
('18', '2', '02', '76.5'),
('1', '1', '01', '65.5'),
('2', '1', '01', '73.0'),
('3', '1', '01', '41.0'),
('4', '1', '01', '39.5'),
('5', '1', '01', '96.0'),
('6', '1', '01', '70.5'),
('7', '1', '01', '80.0'),
('8', '1', '01', '74.0'),
('9', '1', '01', '64.5'),
('10', '1', '02', '55.5'),
('11', '1', '02', '43.0'),
('12', '1', '02', '61.0'),
('13', '1', '02', '49.5'),
('14', '1', '02', '76.0'),
('15', '1', '02', '80.5'),
('16', '1', '02', '99.0'),
('17', '1', '02', '100.0'),
('18', '1', '02', '55.5');
答案 0 :(得分:1)
在这里,你把所有的功课都放在老师手中:D fiddle
SELECT Student.StudentID, Student.FirstName, Student.LastName, Grade.Grade, Subjects.SubjectName
FROM Student INNER JOIN Grade ON Grade.StudentID=Student.StudentID
INNER JOIN Subjects ON Grade.SubjectID=Subjects.SubjectID
WHERE Grade.Grade<50;
SELECT Student.StudentID, Student.FirstName, Student.LastName, AVG(Grade.Grade) AS Average
FROM Student INNER JOIN Grade ON Grade.StudentID=Student.StudentID
GROUP BY Grade.StudentID;
SELECT Subjects.SubjectID, Subjects.SubjectName, AVG(Grade.Grade) AS Average
FROM Subjects INNER JOIN Grade ON Grade.SubjectID=Subjects.SubjectID
GROUP BY Grade.SubjectID;
答案 1 :(得分:0)
这是我的第一次尝试:
SELECT COUNT(*) FROM Grade WHERE Grade <= AVG(Grade);
SELECT AVG(Grade) FROM Grade GROUP BY StudentID;
SELECT AVG(Grade) FROM Grade GROUP BY SubjectID;
可能需要对您的具体情况进行一些调整。顺便说一句,下次你使用Stack Overflow时,发布你的尝试。我们非常感谢您的努力,并将帮助您超越这一特殊情况。此外,复制/粘贴不会让你在毕业后有能力。
关于GROUP BY
:https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html和AVG
:http://www.tutorialspoint.com/mysql/mysql-avg-function.htm。