SQL_Select 使用

来源:互联网 发布:怎么看淘宝订单排名 编辑:程序博客网 时间:2024/04/28 12:17
select  之后可以跟子查询(select * from..)
from 之后可以跟子查询(select * from..)but 要加上 as subtbale;
where 后也可以跟子查询;WHERE子句在聚合前先筛选记录.也就是说作用在GROUP BY 子句和HAVING子句前.

group by :聚类函数; 如果select 后面有sum,max等聚合
函数,在group by 后面的字段一定要包含除了select之后函数的所有字段,并且group by 后面不能够跟 主健  字段, 否则没有意义。
having :: 作用和where 作用相同,但是可以用 count(*)>等的表达式;having对聚合后对组记录进行筛选


eg.  1. 统计所有机构在3月份的每个交易日中,最大交易量,和发生日期

select *  from ( select sum(alltogether)  dayTrans, statdate from   cmis.BranchAppcSum where statdate between '2008-03-01' and '2008-03-31' group by statdate ) as subtbale where dayTrans=(select max(dayTrans) from ( select sum(alltogether)  dayTrans, statdate from   cmis.BranchAppcSum where statdate between '2008-03-01' and '2008-03-31' group by statdate ) as subtbale);

eg . 2. 统计每个机构在3月份交易日中,单日最大交易量  和该月总的交易量

select branchname, max(alltogether) ,sum(alltogether)   from   cmis.BranchAppcSum where statdate between '2008-03-01' and '2008-03-31' group by branchname

eg. 3. 分别统计今年
各个机构所有的交易量:

select branchname ,sum(alltogether) as year_total  from   cmis.BranchAppcSum where statdate between '2008-01-01' and '2008-03-31' group by branchname;

要获得查询记录中,某个字段的值, 方法如下:

select count(*) total from DBTableName;

rs = stmt.executeQuery(sql);// stmt : preparement对象 ;rs: ResultSet 对象;

int totalRows = rs.getLong("total");// 获得返回记录中total 数值;

之后totalRows就是你想要得记录数,只需要一步就能获得你要的结果

如果把所有的纪录都查出来,一行一行移动到最后一行,再取行号,效率也太慢了

遇到有几万几十万纪录的大表,你就该哭了(即使几千条也很慢啊)

而且如果是大表查数据的话,也只是返回一部分,例如返回前100个记录

select first 100 * from DBTableName
原创粉丝点击