检索"01"课程分数小于60,按分数降序排列的学生信息
时间: 2023-06-12 12:04:45 浏览: 127
假设有一个学生信息表student,包含了学生的ID、姓名name、课程编号course、分数score等字段,那么可以使用如下的SQL语句进行检索:
```
SELECT ID, name, course, score
FROM student
WHERE course = '01' AND score < 60
ORDER BY score DESC;
```
其中,`WHERE`子句用于筛选课程编号为'01',且分数小于60的学生信息,`ORDER BY`子句用于将符合条件的学生信息按分数降序排列。
相关问题
-- 9、检索"01"课程分数小于60,按分数降序排列的学生信息
假设有以下三个表:
```
-- 学生表
CREATE TABLE `students` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`age` int(11) NOT NULL,
`gender` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
-- 课程表
CREATE TABLE `courses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
-- 分数表
CREATE TABLE `scores` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;
-- 插入数据
INSERT INTO `students` VALUES (1,'张三',18,'男'),(2,'李四',19,'女'),(3,'王五',20,'男');
INSERT INTO `courses` VALUES (1,'01'),(2,'02');
INSERT INTO `scores` VALUES (1,1,1,80),(2,2,1,70),(3,3,1,59),(4,1,2,90),(5,2,2,75),(6,3,2,85);
```
则可以使用以下SQL语句查询"01"课程分数小于60,按分数降序排列的学生信息:
```
SELECT s.id, s.name, c.name AS course, sc.score
FROM students AS s
INNER JOIN scores AS sc ON s.id = sc.student_id
INNER JOIN courses AS c ON sc.course_id = c.id
WHERE c.name = '01' AND sc.score < 60
ORDER BY sc.score DESC;
```
查询结果为:
```
+----+------+-------+-------+
| id | name | course| score |
+----+------+-------+-------+
| 3 | 王五 | 01 | 59 |
+----+------+-------+-------+
```
检索“c004”课程分数小于60,按分数降序排列的同学学号
假设你要查询的表名为 `scores`,在该表中,学生的学号存储在 `student_id` 字段中,课程编号存储在 `course_id` 字段中,课程分数存储在 `score` 字段中,你可以使用以下 SQL 语句查询“c004”课程分数小于60,按分数降序排列的同学学号:
```
SELECT student_id FROM scores WHERE course_id='c004' AND score<60 ORDER BY score DESC;
```
其中,`scores` 是你要查询的表名,`student_id` 是你要查询的字段,`course_id='c004'` 表示筛选出课程编号为“c004”的记录,`score<60` 表示筛选出分数小于60的记录,`ORDER BY score DESC` 表示按照分数降序排列结果集。这样,你就可以得到“c004”课程分数小于60,按分数降序排列的同学学号了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)