建student-exam表,查看每个学生的志愿学校的个数,查看每个学生humanities-or_sciences中包含哪几门课
时间: 2024-03-11 08:47:41 浏览: 62
大创SikuBERT-for-digital-humanities-and-classical
好的,针对您的问题,我可以给出以下 SQL 语句:
1. 建立student-exam表
```
CREATE TABLE student_exam (
student_id INT,
school_name VARCHAR(50),
subject1 VARCHAR(50),
subject2 VARCHAR(50),
subject3 VARCHAR(50),
humanities_or_sciences VARCHAR(50)
);
```
2. 查看每个学生的志愿学校的个数
```
SELECT student_id, COUNT(DISTINCT school_name) AS num_of_schools
FROM student_exam
GROUP BY student_id;
```
3. 查看每个学生humanities_or_sciences中包含哪几门课
```
SELECT student_id,
CONCAT_WS(', ',
IF(subject1 LIKE CONCAT('%', humanities_or_sciences, '%'), subject1, NULL),
IF(subject2 LIKE CONCAT('%', humanities_or_sciences, '%'), subject2, NULL),
IF(subject3 LIKE CONCAT('%', humanities_or_sciences, '%'), subject3, NULL)
) AS courses
FROM student_exam;
```
其中,`CONCAT_WS` 函数可以将多个字段组合成一个字符串,并用指定的分隔符进行分隔。`IF` 函数用于判断某门课是否包含在 `humanities_or_sciences` 中。
阅读全文