继上一篇创建表的基础上,进行表的简单查询:

 ---------简单查询
--查询水表编号为30408的业主记录
select * from t_Owners where watermeter = '30408'--查询业主名称包含“刘”的业主记录
select * from T_OWNERS where name like '%刘%'--查询业主名称包含‘刘’的且门牌号包含‘5’的业主记录
select * from T_OWNERS where name like '%刘%' and housenumber like '%5%'--查询业主名称包含“刘”的或者门牌号包含“5”的业主记录
select * from T_OWNERS where name like '%刘%' or housenumber like '%5%'--查询业主名称包含“刘”的或者门牌号包含 5 的业主记录,并且地址编号为 3 的记录
select * from T_OWNERS
where (name like '%刘%' or housenumber like '%5%')
and address=3

查询台账记录中用水字数大于等于 10000,并且小于等于 20000 的记录
--我们可以用>= 和<=来实现

select * from T_ACCOUNT
where usernum >= 10000 and usernum <= 20000  --用between。。。and实现
select * from T_ACCOUNT where usernum between 10000 and 20000--查询 T_PRICETABLE 表中 MAXNUM 为空的记录
select * from t_Pricetable where maxnum is null--查询 T_PRICETABLE 表中 MAXNUM 不为空的记录
select * from T_pricetable where maxnum is not null/*去掉重复记录*/
--查询业主表中的地址 ID,不重复显示
select distinct address from T_owners
---排序查询
--对 T_ACCOUNT 表按使用量进行升序排序  默认是升序
select * from T_account order by usernum
--对 T_ACCOUNT 表按使用量进行降序排序
select * from T_account order by usernum desc
--基于伪列的查询
select rowid , t.* from T_ACCOUNT t
select rownum ,t.* from T_ACCOUNT t
--聚合函数
--求和  统计 2012 年所有用户的用水量总和
select sum(usernum) from T_ACCOUNT where year = 2012
--求平均  统计 2012 年所有用水量(字数)的平均值
select avg(usernum) from T_ACCOUNT where year = '2012'
--求最大值 统计 2012 年最高用水量(字数)
select max(usernum) from T_AcCOUNT where year = '2012'
--求最小值  统计 2012 年最低用水量(字数)
select min(usernum) from T_ACCOUNT where year = '2012'
--统计记录个数   统计业主类型 ID 为 1 的业主数量
select count(*) from T_OWNERS where ownertypeid = 1
--分组聚合  按区域分组统计水费合计数
select areaid,sum(money) from T_ACCOUNT group by areaid
--分组后条件查询having  查询水费合计大于 16900 的区域及水费合计
select areaid ,sum(money) from T_ACCOUNT group by areaid having sum(money) > 16900
--连接查询
--多表内连接查询
--查询显示业主编号,业主名称,业主类型名称
select ow.id,ow.name,ot.name from T_Owners ow
inner join t_Ownertype ot
on ow.ownertypeid = ot.id
--第二种方式:
select ow.id,ow.name,ot.name from T_Owners ow ,t_Ownertype ot
where ow.ownertypeid = ot.id
--查询显示业主编号,业主名称、地址和业主类型
select o.id,o.name,ad.name,ot.name from T_OWNERS o ,T_OWNERTYPE ot , T_ADDRESS ad
where o.ownertypeid = ot.id and o.address = ad.id
--查询显示业主编号、业主名称、地址、所属区域、业主分类
select o.id,o.name,ad.name,a.name,ot.name
from T_OWNERS o,T_ADDRESS ad,T_AREA a,T_OWNERTYPE ot
where o.ownertypeid = ot.id and o.address = ad.id and
ad.areaid = a.id
--查询显示业主编号、业主名称、地址、所属区域、收费员、业主分类
select o.id,o.name,ad.name,a.name,op.name,ot.name
from T_OWNERS o , T_ADDRESS ad , T_AREA a ,T_OPERATOR op,T_OWNERTYPE ot
where o.ownertypeid=ot.id and o.address=ad.id and ad.areaid=a.id and ad.operatorid=op.id
--左外链接查询
--查询业主的账务记录,显示业主编号、名称、年、月、金额。如果此业主
--没有账务记录也要列出姓名。
--SQL 1999标准的语法:
select ot.id,ot.name,ac.year,ac.mouth,ac.money
from T_OWNERS ot left join T_ACCOUNT ac
on ot.id=ac.ownerid
--oracle标准的语法:
select ot.id,ot.name,ac.year,ac.mouth,ac.money
from T_OWNERS ot ,T_ACCOUNT ac
where ot.id=ac.ownerid(+)
--右外链接
--SQL 1999标准的语法:
select ot.id,ot.name,ac.year,ac.mouth,ac.money
from T_OWNERS ot right join T_ACCOUNT ac
on ot.id=ac.ownerid
--oracle标准的语法:
select ot.id,ot.name,ac.year,ac.mouth,ac.money
from T_OWNERS ot , T_ACCOUNT ac
where ot.id(+)=ac.ownerid
--where 子句中的子查询
---单行子查询     查询 2012 年 1 月用水量大于平均值的台账记录
//查询2012年1月用水量平均值
select avg(usernum) from T_Account where year=2012 and mouth = 1
select * from T_ACCOUNT where year=2012 and mouth = 1
and usernum > (select avg(usernum) from T_Account where year=2012 and mouth = 1 )
--多行子查询   in 查询地址编号为 1 、3、4 的业主记录
select * from t_Owners where address in (1,3,4)
--查询地址含有“花园”的业主的信息
select * from t_Owners
where address in (select id from t_Address where name like '%花园%')
--查询地址不含有“花园”的业主的信息
select * from t_Owners
where address not in (select id from t_Address where name like '%花园%')
--from子句中的子查询  为多行查询
---查询显示业主编号,业主名称,业主类型名称,条件为业主类型为”居民”,使用子查询实现。
--先查询显示业主编号,业主名称,业主类型名称
select * from
(select  o.id 业主编号,o.name 业主名称,ot.name 业主类型
from t_Owners o,t_ownertype ot
where o.ownertypeid = ot.id
)
where 业主类型='居民'
--select 子句的子查询必须为单行子查询
--列出业主信息,包括 ID,名称,所属地址。
select id,name,(select name from T_ADDRESS where id = address) addressname from t_Owners
--列出业主信息,包括 ID,名称,所属地址,所属区域
select id , name,(select name from T_ADDRESS where id = address) addressname,
(select (select name from T_AREA where id = areaid) from T_ADDRESS where id = address) areaid
from T_OWNERS
--分页查询
--分页查询台账表 T_ACCOUNT,每页 10 条记录
//简单分页
select rownum ,t.* from T_ACCOUNT t where rownum <= 10
--显示大于10 小于20 条记录  需要使用子查询实现 因为rownum只能使用小于或小于等于
select * from
(select rownum r,t.* from T_ACCOUNT t where rownum <= 20)
where r >= 10
--基于排序的分页
---分页查询台账表 T_ACCOUNT,每页 10 条记录,按使用字数降序排序
select * from(
select rownum r ,t.* from (select * from T_ACCOUNT order by usernum desc) t where rownum <= 20
) where r > 10
--单行函数
--字符函数
--求字符串长度 LENGTH
select length('abcd') from dual
--求字符串的子串 SUBSTR
select substr('ABCDEF',2,3) from dual
select substr('ABCDEF',-4,4) from dual
--字符串拼接 CONCAT
select concat('AbcDEF','asd') from dual
select 'ABCD'||'ef' from dual
--数值函数
--四舍五入函数 ROUND
select round('100.234') from dual
select round('100.458',2) from dual
--截取函数 TRUNC
select trunc('100.234') from dual
select trunc('100.234',2) from dual
--取模 MOD
select mod(10,4) from dual
--日期函数
--获取当前时间
select sysdate from dual
--加月函数 ADD_MONTHS :在当前日期基础上加指定的月
select add_months(sysdate,2) from dual
--求所在月最后一天 LAST_DAY
select last_day(sysdate) from dual
--日期截取 TRUNC
select trunc(sysdate) from dual
select trunc(sysdate,'yyyy') from dual--当前年第一天
select trunc(sysdate ,'mm') from dual--当前月第一天
select trunc(sysdate,'dd') from dual--当前日期
--转换函数
--数字转字符串 TO_CHAR
select to_char(1024) from dual
--日期转字符串 TO_CHAR
select to_char(sysdate,'yyyy-mm-dd' ) from dual
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual
--字符串转日期 TO_DATE
select to_date('2017-11-03','yyyy-mm-dd') from dual
--字符串转数字 TO_NUMBER
select to_number('201710') from dual
--其它函数
--空值处理函数 NVL
select nvl(12,0) from dual
--显示价格表中业主类型 ID 为 1 的价格记录,如果上限值为 NULL,则显示 9999999
select nvl(maxnum,9999999),price,minnum from t_Pricetable t where ownertypeid = 1
--空值处理函数 NVL2  NVL2 (检测的值,如果不为 null 的值,如果为 null 的值);
--显示价格表中业主类型 ID 为 1 的价格记录,如果上限值为 NULL,显示“不限”.
select nvl2(maxnum,to_char(maxnum),'不限'),price ,minnum from T_PRICETABLE where ownertypeid = 1
--条件取值 decode   显示下列信息(不要关联查询业主类型表,直接判断 1 2 3 的值)
//第一种方式:
select name ,decode(ownertypeid,1,'居民',2,'行政事业单位',3,'商业') as 类型 from t_Owners
//第二种方式:
select name ,(case ownertypeid
when 1 then '居民'
when 2 then '行政事业单位'
when 3 then '商业'
else '其他'
end)
from t_Owners
//第三种方式:
select name ,(case
when ownertypeid =1 then '居民'
when ownertypeid = 2 then '行政事业单位'
when ownertypeid = 3 then '商业'
end)
from t_Owners
--行列转换  按月份统计 2012 年各个地区的水费
select (select name from T_area where id = areaid) 区域,
sum(case when month = '01' then money else 0 end )一月,
sum(case when month = '02' then money else 0 end )二月,
sum(case when month = '03' then money else 0 end )三月,
sum(case when month = '04' then money else 0 end )四月,
sum(case when month = '05' then money else 0 end )五月,
sum(case when month = '06' then money else 0 end )六月,
sum(case when month = '07' then money else 0 end )七月,
sum(case when month = '08' then money else 0 end )八月,
sum(case when month = '09' then money else 0 end )九月,
sum(case when month = '10' then money else 0 end )十月,
sum(case when month = '11' then money else 0 end )十一月,
sum(case when month = '12' then money else 0 end )十二月
from t_account t where year = 2012 group by areaid
select (select name from T_AREA where id = areaid),
sum(case when month >= '01' and month <= '03' then money else 0 end)第一季度,
sum(case when month >= '04' and month <= '06' then money else 0 end)第二季度,
sum(case when month >= '07' and month <= '09' then money else 0 end)第三季度,
sum(case when month >= '10' and month <= '12' then money else 0 end)第四季度
from T_account where year = '2012' group by areaid
-----------分析函数  rank
--对 T_ACCOUNT 表的 usenum 字段进行排序,相同的值排名相同,排名跳跃
select rank() over(order by usernum desc), usernum from t_Account
--DENSE_RANK 相同的值排名相同,排名连续
---对 T_ACCOUNT 表的 usenum 字段进行排序,相同的值排名相同,排名连续
select dense_rank() over(order by usernum desc),usernum from T_ACCOUNT
---ROW_NUMBER 返回连续的排名,无论值是否相等
--对 T_ACCOUNT 表的 usenum 字段进行排序,返回连续的排名,无论值是否相等
select row_number() over(order by usernum desc),usernum from T_ACCOUNT
---row_number 实现分页查询
select * from(
select row_number() over(order by usernum desc) r,usernum from T_ACCOUNT
)
where r >= 10 and r <= 20
-------集合运算
--------并集运算
---UNION ALL 不去掉重复记录
select * from T_OWners where id > 4
union all
select * from T_OWners where id < 8
--UNION 去掉重复记录
select * from T_OWners where id > 4
union
select * from T_OWners where id < 8
-----交集运算
select * from T_OWners where id > 4
intersect
select * from T_OWners where id < 8
-----差集运算
select * from T_OWners where id > 4
minus
select * from T_OWners where id < 8

												

表的记录查询(基础sql语句)相关推荐

  1. jpa多表联查动态_jpa多表关联动态查询(自定义sql语句)

    项目中,jpa对于简单的数据库操作很方便,但如果多表关联动态查询时,需要自己去写SQL语句拼接查询条件,以下为本人学习的例子. 类似于这种多条件动态查询: 项目用的是springboot 2.1.0. ...

  2. Mysql学习笔记(基础)基础sql语句详细记录

    数据库学习(基础) // 个人网课学习记录,如果有违规等问题,请联系我删除~ mysql下载安装( 解压版安装配置 下载版安装配置 ) 需求分析:使用cmd命令来创建一个数据库,并对数据库中得一张分类 ...

  3. 单表无条件和有条件查询的SQL语句

    话不多说,直接上实验4 简单查询-单表无条件和有条件查询的SQL语句------------ (1) 查询所有学生的基本信息.所有课程的基本信息和所有学生的成绩信息(用三条SQL语句). SELECT ...

  4. mysql数据库中查询第几条到第几条数据_在 mysql 数据库中,从查询结果的第四条记录开始选取5条记录,下面 sql 语句正确的是( )...

    [单选题]同一种货物,在同一线路上或平行线路上作相对方向的运送,而与对方运程的全部或一部分发生重迭交错的运输被称为( ). [多选题]水闸一般由()三部分组成 [单选题]在一个常规的统计表内,非必需包 ...

  5. MySQL基础(八):模糊查询的SQL语句、where条件查询、比较运算符查询、逻辑运算符查询、模糊查询、范围查询、空判断查询

    文章目录 where条件查询 1. where条件查询的介绍 2. 比较运算符查询 3. 逻辑运算符查询 4. 模糊查询 5. 范围查询 6. 空判断查询 7. 小结 where条件查询 学习目标 能 ...

  6. 查询一个表中所有id字段在另一个表中对应值的SQL语句怎么写?

    编辑器加载中... 查询一个表中所有id字段在另一个表中对应值的SQL语句怎么写?多表联结查询:select rbd.RBDID, rbd.ProductCode,p.ProductCnName,p. ...

  7. 数据库基础SQL语句

    文章目录 一.数据库的四大特性ACID: 二.事务 三.终端安装数据库(Docker插件) 1.安装命令 2.检验安装 3.进入服务 4.登录MySQL 5.创建数据库 6.登出 7.退出服务 8.执 ...

  8. mysql多表成绩查询_MySQL多表数据记录查询(一)

    1.交叉连接SQL语句的语法结构如下: select * from表1 cross join 表2; 或 Select * from表1,表2; 2.内连接SQL语句有两种表示形式: 使用inner ...

  9. mysql日期格式化季度_mysql按年度、季度、月度、周、日统计查询的sql语句

    本文介绍一些mysql中用于查询的sql语句,包括按年度.季度.月度.周.日统计查询等,有需要的朋友,可以参考下. 一.年度查询 查询 本年度的数据 SELECT * FROM blog_articl ...

  10. MySQL多表数据记录查询详解

    在实际应用中,经常需要实现在一个查询语句中显示多张表的数据,这就是所谓的多表数据记录连接查询,简称来年将诶查询. 在具体实现连接查询操作时,首先将两个或两个以上的表按照某个条件连接起来,然后再查询到所 ...

最新文章

  1. 原生js实现一个tab栏的标签操作
  2. PS教程第一课:PS简介
  3. python利用thinker制作多页面切换的桌面应用实例教程
  4. 最近想学习一下编译原理,做一个编程规范的检测工具
  5. 实战 | webmagic爬取实战之爬取保险经纪人信息
  6. TFS数据服务器启动优化
  7. 视频专家之路【三】:Vs开发环境的搭建
  8. 微信和QQ,终于可以互通了
  9. 关于 API 定义 安全
  10. CentOS7.2安装linux版QQ
  11. java httpserver stop_java web tomcat服务停止Stopping Coyote HTTP/1.1 on http-8089
  12. sklearn做文本聚类分析
  13. 计算机5级什么水平考试,怎么选择2015计算机等级考试级别
  14. vb.net 教程 5-21 拓展 如何给IE浏览器截图
  15. Shapely的安装
  16. 动态口令,动态密码生成(OTP)
  17. 利用EXCEL批量重命名文件
  18. 计算机协会素拓小游戏,计算机协会素质拓展策划书 (2)
  19. 静态库与动态库的区别(转)
  20. GitLab 搭建 群组Runner

热门文章

  1. 【面试真经】Linux运维面试之内核优化
  2. Java匿名对象的使用
  3. 巧用 mask-image 实现简单进度加载界面
  4. Java---软件试用次数(Properties类的简单使用)
  5. 2022-05-基础日语语法-基本文法
  6. Open3D 纹理贴图
  7. 利用逆矩阵解线性方程组_资料 | 矩阵论简明教程
  8. 牛顿迭代公式是如何推导出来的
  9. 安装VMware时报错:virtualXT,以及虚拟网卡安装失败
  10. 逻辑回归解释 (Logistic Regression)