大家好,又见面了,我是你们的朋友全栈君。
可以说查询是数据库编程中最重要的一环,其用途是从一张表或多张表中检索出满足条件的数据信息
下面开始研究Select语句
Select [ALL DISTINCT] select_list
[INTO new_table]
FROM table_source
[WHERE search_conditions]
[GROUP BY group_by_expression]
[HAVING search_conditions]
[ORDER BY order_expression[ASC DESC]]
ALL 返回列中的所有数据 DISTING 自动过滤列中相同的数据,返回唯一数据
举例: 查询新建的工龄_1的表 Select * from 工龄_1
注: *表示查找表中的所有字段(列)
查询姓名列的所有数据(ALL) Select ALL 姓名 from 工龄_1
注意:当中有两个张三
select distinct 姓名 from 工龄_1
举例:
查询新建的学生成绩表 select * from result_Info
查找语文考试成绩
select * from result_Info
where course_Name='语文'
返回结果如下:
查询考试为语文并且学号为11的成绩 select * from result_Info where course_Name=‘语文’ and student_ID=‘11’
作用:按照指定的条件将数据聚合
举例: 查询新建的学生成绩表 select * from result_Info
查询“2000期中”(条件) 学科的平均分(按照语文,数学进行聚合)
select course_Name,AVG (result) from result_Info
where exam_No='2000期中'
group by course_Name
作用:数据分组后对组的过滤
举例: 查询“2000期中”(条件) 学科的平均分(按照语文,数学进行聚合) 取平均分大于80的数据
select course_Name,AVG (result)
from result_Info
where exam_No='2000期中'
group by course_Name
having AVG(result)>80
作用:将查询的数据进行排序 ASC 为升序 DESC 为降序
举例: 查询考试类型‘2000期中’,学科为语文的成绩
select * from result_Info
where exam_No ='2000期中' and course_Name = '语文'
将数据升序
select * from result_Info
where exam_No ='2000期中' and course_Name = '语文'
Order by result ASC
将数据降序排列
select * from result_Info
where exam_No ='2000期中' and course_Name = '语文'
order by result desc
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/126958.html原文链接:https://javaforall.cn