DBMS 中的 SELECT 语句及其子句
select 语句用于根据条件(如有)从数据库中获取所需数据。这些数据以表格形式返回。
select 语句的基本语法如下:
Select column 1, column 2 ... column N From table_name
select 语句的一个示例如下:
<学生>
| 学号 | 学生姓名 | 学生电话 | 学生成绩 | 学生专业 科目 |
| 1 | Andrew | 6615927284 | 95 | 文学 |
| 2 | Sara | 6583654865 | 65 | 数学 |
| 3 | Harry | 4647567463 | 48 | 文学 |
| 4 | Sally | 6537837084 | 30 | 文学 |
| 5 | Anne | 7457337732 | 88 | 数学 |
查询:
Select Student_Name From Student
此查询产生以下结果:
| 学生姓名 |
| Andrew |
| Sara |
| Harry |
| Sally |
| Anne |
SELECT 语句中的子句
上面给出的 select 语句示例非常简单,在实践中并不实用。因此,select 语句还有许多其他子句,使它更有意义。其中一些是:
WHERE 子句
WHERE 子句用于过滤数据,即它返回满足特定条件的信息。例如:
Select Student_Name From Student Where Student_Marks >50
此查询将返回以下结果
| 学生姓名 |
| Andrew |
| Sara |
| Anne |
GROUP BY 子句
这主要与聚合函数一起使用,以根据列的值对结果集进行分组。例如:
Select Count (Student_Number), Student_MajorSubject From Student Group by Student_MajorSubject
此查询将返回以下结果:
| COUNT(学号) | 学生专业科目 |
| 3 | 文学 |
| 2 | 数学 |
HAVING 子句
这与 GROUP BY 子句一起使用,因为 WHERE 子句不能与聚合函数一起使用。例如:
Select Count(Student_number), Student_MajorSubject From Student Group by Student_MajorSubject Having Count(Student_Number) > 2
此查询将返回以下结果:
| COUNT(学号) | 学生专业科目 |
| 3 | 文学 |
ORDER BY 子句
ORDER BY 关键字用于按升序或降序对结果进行排序。默认情况下,顺序假定为升序。例如:
Select Student_Name From Student Where Student_Marks>50 Order by Student_Marks
此查询将返回以下结果:
| 学生姓名 |
| Sara |
| Anne |
| Andrew |
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP