我必须找出输出作为班级名称,每个班级的学生人数和班级中的平均分数?我的两张桌子都是 -
CREATE TABLE class(Fields_ID INT, Name VARCHAR(20));
INSERT INTO class(Fields_ID,Name) VALUES(30,'FIRST');
INSERT INTO class(Fields_ID,Name) VALUES(31,'SECOND');
INSERT INTO class(Fields_ID,Name) VALUES(32,'THIRD');
INSERT INTO class(Fields_ID,Name) VALUES(33,'FOURTH');
INSERT INTO class(Fields_ID,Name) VALUES(34,'FIFTH');
SELECT * FROM class;
CREATE TABLE student(Fields_ID INT, Name VARCHAR(20));
INSERT INTO student(Fields_ID,Name) VALUES(30,'JYOTI');
INSERT INTO student(Fields_ID,Name) VALUES(31,'KIRTI');
INSERT INTO student(Fields_ID,Name) VALUES(32,'YOGITA');
INSERT INTO student(Fields_ID,Name) VALUES(33,'RASHMI');
INSERT INTO student(Fields_ID,Name) VALUES(34,'NUPUR');
SELECT * FROM student;
CREATE TABLE Marks(Fields_ID INT, Student_ID INT NOT NULL,marks INT NOT NULL);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (30,40,100);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (31,41,88);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (32,42,72);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (33,43,33);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (34,44,15);
SELECT * FROM Marks;
我试图从以下代码返回所需的内容,但我无法形成平均分数的逻辑。知道怎么做。我是MySql的初学者,所以我无法找到问题。
SELECT class.Name , COUNT(student.name),Marks.marks
From class INNER JOIN student INNER JOIN marks
ON class.Fields_ID=student.Fields_ID=Marks.Fields_ID
GROUP BY class.Name;
答案 0 :(得分:1)
SELECT class.Name , COUNT(student.name),avg(Marks.marks) as average_mark
From class
INNER JOIN student on class.Fields_ID = student.Fields_ID
INNER JOIN marks on marks.Fields_ID = student.Fields_ID
GROUP BY class.Name;
答案 1 :(得分:1)
试试这个
SELECT c.Name , COUNT(s.name),m.marks
From class c
INNER JOIN student s on c.Fields_ID = s.Fields_ID
INNER JOIN Marks m on m.Fields_ID = s.Fields_ID
GROUP BY c.Name
order by m.marks
。答案 2 :(得分:1)
试试这个:
SELECT class.Name, COUNT(student.Name), avg(Marks.marks)
FROM class
JOIN student
ON class.Fields_ID = student.Fields_ID
JOIN Marks
ON student.Student_ID = Marks.Student_ID
GROUP BY class.Name;
我对您的表和示例数据进行了一些更改,因为我认为这更符合逻辑。我已将 Student_ID 字段添加到表学生中。因为否则你没有办法唯一地识别你表学生中的行。目前您正在使用Fields_ID
,但我认为这不正确。你想知道学生的标记而不是字段。
示例数据
CREATE TABLE class(Fields_ID INT, Name VARCHAR(20));
INSERT INTO class(Fields_ID,Name) VALUES(30,'FIRST');
INSERT INTO class(Fields_ID,Name) VALUES(31,'SECOND');
INSERT INTO class(Fields_ID,Name) VALUES(32,'THIRD');
INSERT INTO class(Fields_ID,Name) VALUES(33,'FOURTH');
INSERT INTO class(Fields_ID,Name) VALUES(34,'FIFTH');
CREATE TABLE student(Student_ID INT NOT NULL, Fields_ID INT, Name VARCHAR(20));
INSERT INTO student(Student_ID, Fields_ID,Name) VALUES(40, 30,'JYOTI');
INSERT INTO student(Student_ID, Fields_ID,Name) VALUES(41, 31,'KIRTI');
INSERT INTO student(Student_ID,Fields_ID,Name) VALUES(42, 32,'YOGITA');
INSERT INTO student(Student_ID,Fields_ID,Name) VALUES(43, 33,'RASHMI');
INSERT INTO student(Student_ID,Fields_ID,Name) VALUES(44, 34,'NUPUR');
CREATE TABLE Marks(Fields_ID INT, Student_ID INT NOT NULL,marks INT NOT NULL);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (30,40,100);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (31,41,88);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (32,42,72);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (33,43,33);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (34,44,15);
编辑:
我认为目前您的表格Marks
会有PRIMARY KEY
名为Fields_ID
。这不起作用,因为会有比每个字段更多的标记。因此,我建议将表格PRIMARY KEY
中的marks
更改为Marks_ID int auto_increment
。如果您不想创建seprate键来标识行并使用compiste键(PK:Student_ID
,Fields_ID
),则可以。 BTW不要忘记更改学生的主键。
这个表结构包含样本数据,包括主键:
CREATE TABLE class(Fields_ID INT PRIMARY KEY, Name VARCHAR(20));
INSERT INTO class(Fields_ID,Name) VALUES(30,'FIRST');
INSERT INTO class(Fields_ID,Name) VALUES(31,'SECOND');
INSERT INTO class(Fields_ID,Name) VALUES(32,'THIRD');
INSERT INTO class(Fields_ID,Name) VALUES(33,'FOURTH');
INSERT INTO class(Fields_ID,Name) VALUES(34,'FIFTH');
CREATE TABLE student(Student_ID INT NOT NULL PRIMARY KEY, Fields_ID INT, Name VARCHAR(20));
INSERT INTO student(Student_ID, Fields_ID,Name) VALUES(40, 30,'JYOTI');
INSERT INTO student(Student_ID, Fields_ID,Name) VALUES(41, 31,'KIRTI');
INSERT INTO student(Student_ID,Fields_ID,Name) VALUES(42, 32,'YOGITA');
INSERT INTO student(Student_ID,Fields_ID,Name) VALUES(43, 33,'RASHMI');
INSERT INTO student(Student_ID,Fields_ID,Name) VALUES(44, 34,'NUPUR');
CREATE TABLE Marks(Marks_ID INT auto_increment PRIMARY KEY, Fields_ID INT, Student_ID INT NOT NULL,marks INT NOT NULL) ;
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (30,40,100);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (31,41,88);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (32,42,72);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (33,43,33);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (34,44,15);