美文网首页
数据的基本查询

数据的基本查询

作者: 承_风 | 来源:发表于2019-07-21 22:52 被阅读0次

理解查询

服务器执行命令,在原始数据表中查找符合条件的数据,产生一个虚拟表。
虚拟表是数据组合后的重新展示,而不是原始的物理数据。

查询基本语法构成

select <列名>
from <表名>
[where <查询条件表达式>]
[order by <排序的列名> ASC或DESC]

查询全部行和列

select * from Students

查询部分行

select StudentName,Gender,出生日期
from Students where Gender='男' and Age>20

使用“AS”或使用“=”重新命名字段

select StudentName as 姓名,Gender as 性别,出生日期=birthday
from Students where Gender='男'

加号的使用

select 姓名=StudentName,地址和电话=StudentAddress+'【'+PhoneNumber+'】' 
from Students where Gender='男'
select 总成绩=CSharp+SQLServerDB from ScoreList

注意:
1.+连接的数据类型必须兼容
2.如果使用+连接字符型数据,结果为字符串数据的连接
3.如果使用+连接数值型数据,结果为数值的和

查询空列

select * from ScoreList where SQLServerDB is null

使用常量列

select StudentName as 姓名,Gender as 性别,出生日期=birthday,所在学校='北京大学'
from Students where Gender='男'

限制固定行数

select top 5 StudentName,Gender,Birthday from Students

返回百分之多少行

select top 20 percent StudentName,Gender,Birthday from Students

按多列排序

select top 3 StudentId,CSharp as C#,DB=SQLServerDB
from ScoreList  
where StudentId not in(select top 6 StudentId from ScoreList order by SQLServerDB DESC,CSharp DESC )
order by SQLServerDB DESC,CSharp DESC

模糊查询-like

select StudentName,StudentAddress from Students where StudentAddress like '天津%'
select StudentName,StudentAddress from Students where StudentName like '%小%'

模糊查询-between

select * from ScoreList where CSharp between 80 and 90
select StudentName,StudentAddress,Birthday from Students where Birthday between '1987-01-01' and '1988-01-01'

模糊查询-in

select StudentName,StudentAddress,age from Students where Age in(21,22,23)
select StudentName,StudentAddress,age from Students where StudentName in('王小虎','贺小张')

查询函数的使用

聚合函数(求和、统计、求最大值、最小值、平均值)

select SUM(CSharp) as C#总成绩 from ScoreList
select 总人数=COUNT(*) from Students
select MAX(Csharp) as C#最高分 ,MIN(CSharp) as C#最低分,AVG(CSharp) as C#平均分 from ScoreList

多表之间的数据查询

内连接(inner join...on...)查询
select Students.StudentId,C#成绩=CSharp,StudentName,ClassName
from ScoreList
inner join Students on Students.StudentId=ScoreList.StudentId
inner join StudentClass on Students.ClassId=StudentClass.ClassId
where CSharp >80
左外连接(left outer join...on...)
select Students.StudentId,StudentName,Gender ,C#成绩=CSharp from Students
left outer join ScoreList on Students.StudentId=ScoreList.StudentId
where Gender='男'
右外连接(right outer join...on...)

分组查询、统计及统计筛选(group by...having...)

select 班级=StudentClass.ClassName,人数=COUNT(*),C#最高分=Max(CSharp),DB最高分=MAX(SQLServerDB),
AVG(CSharp) as C#平均分,AVG(SQLServerDB) as DB平均分
from Students
inner Join StudentClass on Students.ClassId =StudentClass.ClassId
inner join ScoreList on ScoreList.StudentId=Students.StudentId
group by ClassName
having AVG(CSharp)>=70 and AVG(SQLServerDB)>=70

查询重复

--查询所有重复的记录
select * from ScoreList
where StudentId in(select StudentId from ScoreList group by StudentId  having COUNT(*)>1)
order by StudentId
--其它方法
select * from ScoreList
where (select COUNT(*) from ScoreList s where s.StudentId=ScoreList.StudentId)>1
order by StudentId

分组查询对比

  • where字句:从数据源中去掉不符合其搜索条件的数据。
  • group by字句:搜集数据行到各个组中,统计函数为各个组计算统计值。
  • having字句:在分组结果中,去掉不符合其组搜索条件的各组数据行。

相关文章

  • SQL之DQL

    DQL用于从数据库查询数据,并不会修改数据 基本查询 条件控制 查询排序 分组查询 limit限制 一.基本查询 ...

  • 关系数据库SQL之高级数据查询:去重复、组合查询、连接查询、虚拟

    前言 接上一篇关系数据库SQL之基本数据查询:子查询、分组查询、模糊查询,主要是关系型数据库基本数据查询。包括子查...

  • spring date mongo mongotemplate使

    Spring数据MongoDB三:基本文档查询(查询,基本查询)(一) MongoDB高级查询[聚合] sprin...

  • 数据的基本查询

    理解查询 服务器执行命令,在原始数据表中查找符合条件的数据,产生一个虚拟表。虚拟表是数据组合后的重新展示,而不是原...

  • MySql基本操作语句-查询篇

    查询基本操作 1.基本查询 2.查询表中的特定字段 3.查询其他数据库下的表好处就是可以不要打开其数据库 *.查询...

  • OCP课程基础总结一

    基本命令 基本的select查询语句 基本函数查询语句 条件查询语句 使用联接显示多个表中的数据 使用子查询来解决...

  • es header 简单使用

    1、可以在基本查询里查看查询语句 2、自己查询根据之前的格式自己调整后查询 基本语句1、清空索引里数据

  • 4.SQL之数据查询(DQL)

    DQL--数据查询语言 谓词:select 查询数据 基本语法 基础查询 查询多个字段 去除重复记录 起别名 条件...

  • MySQL 从零开始学(五)查询数据

    基本查询语句 MySQL 从数据库查询的基本语句为 SELECT,基本格式是: {* | <字段列表>} 星号是通...

  • MongoDB数据查询

    数据的查询 基本查询 l 查询的核心语法 | db.infos.find(查询条件,[,{设置显示的字段}]) |...

网友评论

      本文标题:数据的基本查询

      本文链接:https://www.haomeiwen.com/subject/qwkblctx.html