有以下三个表:(共10分,每题5分) student(student_id,student_name,student_age) 分别代表:学号,学生姓名,所属单位,学习年龄 course(course_id,course_name) 分别代表:课程编号,课程名称 stu_cour(student_id,course_id,grade) 分别代表:学号,所选课程编号,学习成绩 请写出相应的sql语句: 1.统计选修了课程的人数(选修多门时,只计算一次) 2.查询选课在3门以上(包括3门)的学生,要求显示学号,姓名,平均分,选课门数,按平均分降序排序
时间: 2023-03-13 19:16:53 浏览: 115
1 SELECT COUNT(DISTINCT student_id) FROM stu_cour; 2. SELECT student_id,student_name,AVG(grade) AS avg_grade,COUNT(*) AS course_nums FROM student JOIN stu_cour USING(student_id) GROUP BY student_id HAVING COUNT(*) >= 3 ORDER BY avg_grade DESC;
相关问题
改错#include<iostream> #include<string.h> using namespace std; class Teacher{ public: string teacher_name; int teacher_age; string teacher_duty; teacher(string teacher_name,int teacher_age){ this->teacher_name=teacher_name; this->teacher_age=teacher_age; teacher_duty="教学"; } }; class Student{ public: string student_name; int student_id; string student_duty; student(string student_name,int student_id){ this->student_name=student_name; this->student_id=student_id; student_duty="学习"; } }; class Graduate{ public: string graduate_name; int graduate_age; int graduate_id; string graduate_duty; graduate(string graduate_name,int graduate_age,int graduate_id){ this->graduate_name=graduate_name; this->graduate_age=graduate_age; this->graduate_id=graduate_id; graduate_duty="教学和学习"; } }; int main(){ Teacher teacher("Tom", 35); Student student("John", 2021000); Graduate graduate("Mary", 25, 20210002); cout << teacher.teacher_name << " " << teacher.teacher_age<< " " << teacher.teacher_duty<< endl; cout << student.student_name<< " " << student.student_id << " " << student.student_duty<< endl; cout << graduate.graduate_name<< " " << graduate.graduate_age << " " << graduate.graduate_id<< " " << graduate.graduate_duty<< endl; return 0; }
在类的定义中,构造函数的名称应该与类名完全相同,而你的代码中构造函数的名称与类名不一致,需要修改为以下代码:
```c++
#include<iostream>
#include<string.h>
using namespace std;
class Teacher{
public:
string teacher_name;
int teacher_age;
string teacher_duty;
Teacher(string teacher_name,int teacher_age){
this->teacher_name=teacher_name;
this->teacher_age=teacher_age;
teacher_duty="教学";
}
};
class Student{
public:
string student_name;
int student_id;
string student_duty;
Student(string student_name,int student_id){
this->student_name=student_name;
this->student_id=student_id;
student_duty="学习";
}
};
class Graduate{
public:
string graduate_name;
int graduate_age;
int graduate_id;
string graduate_duty;
Graduate(string graduate_name,int graduate_age,int graduate_id){
this->graduate_name=graduate_name;
this->graduate_age=graduate_age;
this->graduate_id=graduate_id;
graduate_duty="教学和学习";
}
};
int main(){
Teacher teacher("Tom", 35);
Student student("John", 2021000);
Graduate graduate("Mary", 25, 20210002);
cout << teacher.teacher_name << " " << teacher.teacher_age<< " " << teacher.teacher_duty<< endl;
cout << student.student_name<< " " << student.student_id << " " << student.student_duty<< endl;
cout << graduate.graduate_name<< " " << graduate.graduate_age << " " << graduate.graduate_id<< " " << graduate.graduate_duty<< endl;
return 0;
}
```
检查代码#include<iostream> #include<string.h> using namespace std; class Teacher{ public: string teacher_name; int teacher_age; string teacher_duty; teacher(string teacher_name,int teacher_age){ this->teacher_name=teacher_name; this->teacher_age=teacher_age; teacher_duty="教学"; } }; class Student{ public: string student_name; double student_id; string student_duty; student(string student_name,double student_id){ this->student_name=student_name; this->student_id=student_id; student_duty="学习"; } }; class Graduate{ public: string graduate_name; int graduate_age; double graduate_id; string graduate_duty; graduate(string graduate_name,int graduate_age,double graduate_id){ this->graduate_name=graduate_name; this->graduate_age=graduate_age; this->graduate_id=graduate_id; graduate_duty="教学和学习"; } }; int main(){ Teacher teacher("Tom", 35); Student student("John", "20210001"); Graduate graduate("Mary", 25, "20210002"); cout << teacher.teacher_name << " " << teacher.teacher_age<< " " << teacher.teacher_duty<< endl; cout << student.student_name<< " " << student.student_id << " " << student.student_duty<< endl; cout << graduate.graduate_name<< " " << graduate.graduate_age << " " << graduate.graduate_id<< " " << graduate.graduate_duty<< endl; return 0; }
这段代码存在一些问题:
1. 在类的声明中,应该添加访问修饰符,比如public、protected、private等,否则默认为private。
2. 在Student类中,student_id应该是int类型而不是double类型。
3. 在Graduate类中,graduate_id应该是int类型而不是double类型。
4. 在main函数中,创建Student和Graduate对象时,传入的参数类型不正确,应该是整型而不是字符串类型。
5. 在Graduate类中,graduate_duty的赋值应该为"教学和学术",而不是"教学和学习"。
修复后的代码如下:
```
#include<iostream>
#include<string.h>
using namespace std;
class Teacher{
public:
string teacher_name;
int teacher_age;
string teacher_duty;
Teacher(string teacher_name,int teacher_age){
this->teacher_name=teacher_name;
this->teacher_age=teacher_age;
teacher_duty="教学";
}
};
class Student{
public:
string student_name;
int student_id;
string student_duty;
Student(string student_name,int student_id){
this->student_name=student_name;
this->student_id=student_id;
student_duty="学习";
}
};
class Graduate{
public:
string graduate_name;
int graduate_age;
int graduate_id;
string graduate_duty;
Graduate(string graduate_name,int graduate_age,int graduate_id){
this->graduate_name=graduate_name;
this->graduate_age=graduate_age;
this->graduate_id=graduate_id;
graduate_duty="教学和学术";
}
};
int main(){
Teacher teacher("Tom", 35);
Student student("John", 20210001);
Graduate graduate("Mary", 25, 20210002);
cout << teacher.teacher_name << " " << teacher.teacher_age<< " " << teacher.teacher_duty<< endl;
cout << student.student_name<< " " << student.student_id << " " << student.student_duty<< endl;
cout << graduate.graduate_name<< " " << graduate.graduate_age << " " << graduate.graduate_id<< " " << graduate.graduate_duty<< endl;
return 0;
}
```
阅读全文