日记大全

日记大全 > 句子大全

分享SQL语句查询表中数据的实例操作解析

句子大全 2023-05-23 07:41:02
相关推荐

今天和大家分享下用SQL语句查询表的各种操作。主要有:给字段取别名、Top的用法、Distinct的用法、COUNT的用法、MAX的用法、MIN的用法、SUM的用法、AVG的用法、COUNT的用法、Between 的用法。

字段取别名

给字段取别名共有三种方式:

第一种方式

例句:select Stuname as "姓名",StuAge as "年龄" from student

第二种方式

例句:select Stuname as 姓名,StuAge as 年龄 from student

第三种方式

例句:select 名字=StuAge,年龄=StuAge from student

TOP的用法

1、查询所有的数据中的前10条数据

例句:select top 10 * from student

2、查询年龄最小的5个学生的信息

例句:select top 5 * from student order by Stuage

3、从大到小排序,查询的是年龄最大的百分之10的学生信息

例句:select top 10 percent* from student order by Stuage desc

去除重复的数据

去除重复的数据用distinct关键字

例句:select distinct StuName from student

MAX的用法

查询年龄最高的学生记录

例句:select MAX(Stuage) from student

MIN的用法

查询年龄最小的学生记录

例句:select MIN(Stuage) from student

SUM的用法

将所有学生的年龄加总

例句:select SUM(Stuage) from student

AVG的用法

求学生表中年龄字段的平均值

例句:select AVG(Stuage) from student

COUNT的用法

统计学生表中行的总记录数

例句:select COUNT(*) from student

BETWEEN 的用法

统计年龄在19至29岁的学生的记录

例句:select * from student where Stuage between 19 and 29

IN、NOT IN的用法

统计学生姓名是李四,张三的记录

例句:select * from student where StuName in("李四","张三")

统计学生姓名不是李四,张三的记录

例句:select * from student where StuName not in("李四","张三")

以上就是和大家分享的用SQL语句查询表的各种操作。相对来说比较简单,只要记住这些关键字,基本上也就掌握了它的用法。

阅读剩余内容
网友评论
相关内容
拓展阅读
最近更新