CREATE TABLE student ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '学生ID', name VARCHAR(50) NOT NULL COMMENT '学生姓名', gender VARCHAR(10) NOT NULL COMMENT '学生性别', birthday DATE NOT NULL COMMENT '学生生日', address VARCHAR(100) NOT NULL COMMENT '学生住址', phone VARCHAR(20) NOT NULL COMMENT '学生联系方式' ) COMMENT '学生信息表'; 科目表:
CREATE TABLE course ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '科目ID', name VARCHAR(50) NOT NULL COMMENT '科目名称', teacher VARCHAR(50) NOT NULL COMMENT '授课教师', credit INT NOT NULL COMMENT '科目学分' ) COMMENT '科目表'; 学生选修科目表:
CREATE TABLE student_course ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '关系ID', student_id INT NOT NULL COMMENT '学生ID', course_id INT NOT NULL COMMENT '科目ID', FOREIGN KEY (student_id) REFERENCES student(id), FOREIGN KEY (course_id) REFERENCES course(id) ) COMMENT '学生选修科目表'; 学生成绩表:
CREATE TABLE score ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '成绩ID', student_id INT NOT NULL COMMENT '学生ID', course_id INT NOT NULL COMMENT '科目ID', score INT NOT NULL COMMENT '成绩', FOREIGN KEY (student_id) REFERENCES student(id), FOREIGN KEY (course_id) REFERENCES course(id) ) COMMENT '学生成绩表';
## ---自然语言转换:--- SELECT score.score FROM score INNER JOIN student_course ON score.course_id = student_course.course_id INNER JOIN student ON student_course.student_id = student.id WHERE student.name = '小明' ## --- END ---
再比如:”查询总成绩排名班级前十的同学” 生成结果如下:
1 2 3 4 5 6 7 8 9 10 11
## ---BEGIN--- ## 查询总成绩排名班级前十的同学
## ---自然语言转换:--- SELECT s.name, SUM(sc.score) AS total_score FROM student s INNER JOIN student_course sc ON s.id = sc.student_id GROUP BY s.id ORDER BY total_score DESC LIMIT 10; ## --- END ---
## ---BEGIN--- ## SELECT s.name, SUM(sc.score) AS total_score FROM student s INNER JOIN student_course sc ON s.id = sc.student_id GROUP BY s.id ORDER BY total_score DESC LIMIT 10; ## ---SQL转换:--- SQL转换:
SELECT s.name, SUM(sc.score) AS total_score FROM student s INNER JOIN student_course sc ON s.id = sc.student_id GROUP BY s.id, s.name ORDER BY total_score DESC FETCH FIRST 10 ROWS ONLY; ## --- END ---