首页 > 技术 > 实用SQL大集结(查询篇)

实用SQL大集结(查询篇)

2010年1月4日 QiQi 39 次浏览 发表评论 阅读评论

总结SQL 查询篇

当然我不是权威小文也可能有误,欢迎互相交流学习和指教。
DB2中为防止锁定表,需在select 语句后加上 with ur,切记!!

——————————————————————————-
查询记录总数,第一条会比第二条执行的效率更高,屡试不爽
Select count(1) from users;
Select count(*) from users;

——————————————————————————–
查询总数,去掉某字段的值重复的项
例子查询出来的内容:一共有多少条username的记录,并且相同的username仅记作一条

select count(distinct username) from users ;

——————————————————————————–
查询条件:某一字段为空
Select * from users where type is null;

——————————————————————————–
查询条件:某字段长度为20
Select * from users where length(type)=20;

——————————————————————————–
查询条件:两个条件中任意一个条件成立即查询出来
Select * from users where (type = ‘S’ or type = ‘P’) and meg = ‘G’;

——————————————————————————–
导入/导出数据,前提是两者的表结构需完全相同 (For DB2)
export to d:\download\list.txt of del select * from users where OPNDTE>=’2009-12-08′;
Import from d:\download\list.txt of del insert into users where OPNDTE>=’2009-12-08′;

如果需要replace整个表那么就要使用这样的命令,如果需要导入的数据量特别大,那 么使用commitcount这个命令就可以在执行的过程中看到每导入2000行数据就会显示 出一行提示信息,以了解目前数据到底导入了多少,进度如何
import from d:\download\list.txt of del commitcount 2000 replace into users;

但我们有时候会遇到某一个字段特别长的情况,比如超过500,那么导出成另一种格式的文件就尤其必要,但是这种文件就无法使用UltraEdit等工具打开
export to d:\download\list.ixf of ixf select * from users where OPNDTE>=’2009-12-08′;
Import from d:\download\list.ixf of ixf insert into users where OPNDTE>=’2009-12-08′;

假如导出的表和将要导入的表的表结构不同,那么需要做相应的转换或者调试,那么最 好导出为TXT文件,可以进入文件中进行数字型或者Char型的调试,比如目标表仅有 四个字段,第四个字段默认都是0数字型
Export to D:\download\list.txt of del select a, b, c, 0 from users where type = ‘S’;
Import from D:\download\list.txt of del insert into users_bak;

如果需要导出Excel格式类型的文件,那么使用下面的语句就可以了
Export to D:\download\list.csv of del select a, b, c, 0 from users where type = ‘S’;

——————————————————————————–
查询条件: 有多个符合项
Select * from users where type in (’A',’F',’H',’O');

——————————————————————————–
查询某一字段的值,在整表数据中,拥有相同的值的项总数超过1个的(即:大于等于2)情况有多少个/有哪些值全部列出来
Select count(*) as sum from users group by tel having count(tel) > 1;
Select tel from users group by tel having count(tel) >1 ;

——————————————————————————–
查询中使用不等于符号
Select * from users where tel <> ” and type is not null;

——————————————————————————–
查询中使用左连接和右连接
表A左连接表B:A的整个+A/B交集的部分
表A右连接表B:B的整个+A/B交集的部分
举例左连接SQL的写法
select a.*, b.prvnam from users a left join province b on a.PRVCDE=b.PRVCDE where (a.prvcde in (’BJ’,'SH’) or b.CTRCDE in (’1′,’2′,’3′)) and a.type in (’P',’S');

——————————————————————————–
查询中如何使用trim函数去空字符,当需要匹配两个字段的值的前面/后面有空格时,必须要去掉空格才能进行比较,很类似于程序的trim()方法
左去空: ltrim()
右去空: rtrim()
Select * from users a, prov b where a.prvcde = ltrim(b.ctrcde) order by a.prvcde desc;

——————————————————————————–
查询中如何使用current Date 获取当前日期
select * from users where username=’July’ and type in (’female’) and expireDate>=(year(current date)*100+month(current date))*100 + day(current date);

——————————————————————————–
如何进行模糊查询
select * from users where username like ‘%test%’;

——————————————————————————–
如何使或(||)函数,可连接两个char类型的字符串
select rtrim(CHAR(create_date))||’ 00:00:00.000000′ from users where username=’July’ ;
select * from users where uyy||umm=201001;

——————————————————————————–
使用函数max() / min() / sum()
Max() 取最大值
Min() 取最小值
Sum() 取加总值
Select max(amount) as maxAmount from users where type not in (’Y');
Select min(amount) as minAmount from users where type like ‘Y’;
Select sum(amount) as sumAmount from users where type not like ‘Y’;

——————————————————————————–
使用函数substr(a, b, c),a为字段名,b为从左起数的第b位,c为长度
select substr(a.CNTCDE,3,2), b.ctynam, a.CNTNAM from city a left join province b on (substr(a.CNTCDE,3,2))=ltrim(b.CNTCDE) where a.status=’A’ and right(left(a.CNTCDE,3),1)<’A’ order by substr(a.CNTCDE,3,2);

——————————————————————————–
查询第一行的记录,使用rownum (For Oracle)
select * from (select * from users where month=to_date(’201001′,’yyyymm’) and seq=’1′ order by id desc) where rownum=1;

——————————————————————————–
当前考评年度,当前月份大于等于9月,那么当前的考评年度的范围即为:当前年的9月起至下一年的8月止;当前月份小于等于8月,那么当前的考评年度的范围即为:上一年的9月起至当前年的8月止 (For Oracle)
to_char(month,’yyyyMM’) between (
CASE WHEN TO_CHAR (sysdate, ‘MM’) < ‘09′
THEN TO_CHAR ( ADD_MONTHS (sysdate,-12),’yyyy’) || ‘09′
ELSE TO_CHAR ( sysdate,’yyyy’) || ‘09′
END
)
and
(CASE
WHEN TO_CHAR (sysdate, ‘MM’) < ‘09′
THEN TO_CHAR ( sysdate, ‘yyyy’
) || ‘08′
ELSE TO_CHAR ( ADD_MONTHS(sysdate,12),
‘yyyy’
) || ‘08′
END)

转载原创文章请注明:文章转载自:嗨!柒柒! 阅读 影音 旅行 生活 分享 (http://www.jiangyouni.cn/qiqi)
本文标题:实用SQL大集结(查询篇)
本文地址:http://www.jiangyouni.cn/qiqi/?p=288

分类: 技术 标签: , ,
  1. 2010年1月8日21:18 | #1

    看不懂,还是要支持face11

    [回复]

  2. 2010年2月5日14:07 | #2

    face4真深奥,看不懂啊啊face3

    [回复]

    QiQi 回复:

    喂,你都会看不懂,别来拆我的台了。不是在嘲笑我吧,哼…真是的,一边玩去,去去去…

    [回复]

  3. 2010年3月1日12:56 | #3

    face9face9玩去了哦!!

    [回复]

  1. 本文目前尚无任何 trackbacks 和 pingbacks.