sql server基本语句 2008 查询结果如何不显示 具体语句就显示结果

【SQL】查询语句中in和exists的区别
【SQL】查询语句中in和exists的区别
in可以分为三类:
select * from t1 where f1 in ( 'a ', 'b ')
,应该和以下两种比较效率
select * from t1 where f1= 'a ' or f1= 'b '
select * from t1 where f1 = 'a ' union all select * from t1 f1= 'b '
  你可能指的不是这一类,这里不做讨论。
select * from t1 where f1 in (select f1 from t2 where t2.fx= 'x ')
  其中子查询的where里的条件不受外层查询的影响,这类查询一般情况下,自动优化会转成exist语句,也就是效率和exist一样。
select * from t1 where f1 in (select f1 from t2 where t2.fx=t1.fx)
  其中子查询的where里的条件受外层查询的影响,这类查询的效率要看相关条件涉及的字段的索引情况和数据量多少,一般认为效率不如exists.
  除了第一类in语句都是可以转化成exists 语句的,一般习惯应该是用exists而不用in.
  A,B两个表,
  (1)当只显示一个表的数据如A,关系条件只一个如ID时,使用IN更快:
 select * from A where id in (select id from B)
  (2)当只显示一个表的数据如A,关系条件不只一个如ID,col1时,使用IN就不方便了,可以使用EXISTS:
  select * from A
  where exists (select 1 from B where id = A.id and col1 = A.col1)
  (3)当只显示两个表的数据时,使用IN,EXISTS都不合适,要使用连接:
 select * from A left join B on id = A.id
  所以使用何种方式,要根据要求来定。
exists是用来判断是否存在的,当exists(查询)中的查询存在结果时则返回真,否则返回假。not exists则相反。
exists做为where 条件时,是先对where前的主查询询进行查询,然后用主查询的结果一个一个的代入exists的查询进行判断,如果为真则输出当前这一条主查询的结果,否则不输出。
in和exists区别
in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询。
一直以来认为exists比in效率高的说法是不准确的。
如果查询的两个表大小相当,那么用in和exists差别不大。
如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in。
NOT EXISTS,exists的用法跟in不一样,一般都需要和子表进行关联,而且关联时,需要用索引,这样就可以加快速度。
exists 相当于存在量词:表示集合存在,也就是集合不为空只作用一个集合。
例如 exist P 表示P不空时为真; not exist P表示p为空时为真。&
in表示一个标量和一元关系的关系。
例如:s in P表示当s与P中的某个值相等时 为真; s not in P 表示s与P中的每一个值都不相等时为真:
not in 和not exists的区别
如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;
而not extsts 的子查询依然能用到表上的索引。
所以无论那个表大,用not exists都比not in要快。
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'如何将多个SQL查询统计结果一次显示出来
我们经常会碰到各种分类统计,有时需要将这些统计结果一次显示出来,并计算分类统计占总量的比例,例如:一段时间内每日设备销售总量、台式机销量、笔记本销量等,我的工作中也碰到类似问题,我们需要统计一段时间内邮件收寄总量、自收总量、妥投总量、自投总量和自收自投总量等,以统计-22日合肥到安庆邮件为例,其查询语句如下:
--收寄总量:
select a.clct_date,count(*) from tb_evt_mail_clct a
&where a.clct_date&=to_date(';,'yyyymmdd') and a.clct_date&=to_date(';,'yyyymmdd')
&and exists (select 1 from tb_jg b where b.zj_code=a.clct_bureau_org_code and b.city ='合肥市')
&and exists (select 1 from tb_jg d where d.xs_code=substr(a.rcv_area,1,4) and d.city ='安庆市')
& group by a.clct_date order by a.clct_
--自收总量:
select a.clct_date,count(*) from tb_evt_mail_clct a
&where a.clct_date&=to_date(';,'yyyymmdd') and a.clct_date&=to_date(';,'yyyymmdd')
&and exists (select 1 from sncn_zd_jg b where b.zj_code=a.clct_bureau_org_code and b.city ='合肥市')
&and exists (select 1 from tb_jg d where d.xs_code=substr(a.rcv_area,1,4) and d.city ='安庆市')
& group by a.clct_date order by a.clct_
--妥投总量:
select a.clct_date,count(*) from tb_evt_mail_clct a,tb_evt_dlv c
&where a.clct_date&=to_date(';,'yyyymmdd') and a.clct_date&=to_date(';,'yyyymmdd')
& and a.mail_num=c.mail_num and c.dlv_sts_code='I'
&and exists (select 1 from tb_jg b where b.zj_code=a.clct_bureau_org_code and b.city ='合肥市')
&and exists (select 1 from tb_jg d where d.zj_code=c.dlv_bureau_org_code and d.city ='安庆市')
& group by a.clct_date order by a.clct_
--自投总量:
select a.clct_date,count(*) from tb_evt_mail_clct a,tb_evt_dlv c
&where a.clct_date&=to_date(';,'yyyymmdd') and a.clct_date&=to_date(';,'yyyymmdd')
& and a.mail_num=c.mail_num and c.dlv_sts_code='I'
&and exists (select 1 from tb_jg b where b.zj_code=a.clct_bureau_org_code and b.city ='合肥市')
&and exists (select 1 from sncn_zd_jg d where d.zj_code=c.dlv_bureau_org_code and d.city ='安庆市')
& group by a.clct_date order by a.clct_
--自收自投总量:
&select a.clct_date,count(*)& from tb_evt_mail_clct a,tb_evt_dlv c
&where a.clct_date&=to_date(';,'yyyymmdd') and a.clct_date&=to_date(';,'yyyymmdd')
& and a.mail_num=c.mail_num and c.dlv_sts_code='I'
&and exists (select 1 from sncn_zd_jg b where b.zj_code=a.clct_bureau_org_code and b.city ='合肥市')
&and exists (select 1 from sncn_zd_jg d where d.zj_code=c.dlv_bureau_org_code and d.city ='安庆市')
& group by a.clct_date order by a.clct_
将每个查询结果看着是一个表,使用join关键字将所有的查询连接起来,就可以一次显示所有查询结果,语句如下:
select aa.rq,aa.sjzl,bb.zszl,cc.ttzl,dd.ztzl,ee.zszt from
(select a.clct_date rq,count(*) sjzl from tb_evt_mail_clct a
&where a.clct_date&=to_date(';,'yyyymmdd') and a.clct_date&=to_date(';,'yyyymmdd')
&and exists (select 1 from tb_jg b where b.zj_code=a.clct_bureau_org_code and b.city ='合肥市')
&and exists (select 1 from tb_jg d where d.xs_code=substr(a.rcv_area,1,4) and d.city ='安庆市')
& group by a.clct_date order by a.clct_date) aa
(select a.clct_date rq,count(*) zszl from tb_evt_mail_clct a
&where a.clct_date&=to_date(';,'yyyymmdd') and a.clct_date&=to_date(';,'yyyymmdd')
&and exists (select 1 from sncn_zd_jg b where b.zj_code=a.clct_bureau_org_code and b.city ='合肥市')
&and exists (select 1 from tb_jg d where d.xs_code=substr(a.rcv_area,1,4) and d.city ='安庆市')
& group by a.clct_date order by a.clct_date) bb
& on aa.rq=bb.rq
(select a.clct_date rq,count(*) ttzl from tb_evt_mail_clct a,tb_evt_dlv c
&where a.clct_date&=to_date(';,'yyyymmdd') and a.clct_date&=to_date(';,'yyyymmdd')
& and a.mail_num=c.mail_num and c.dlv_sts_code='I'
&and exists (select 1 from tb_jg b where b.zj_code=a.clct_bureau_org_code and b.city ='合肥市')
&and exists (select 1 from tb_jg d where d.zj_code=c.dlv_bureau_org_code and d.city ='安庆市')
& group by a.clct_date order by a.clct_date) cc
& on aa.rq=cc.rq
(select a.clct_date rq,count(*) ztzl from tb_evt_mail_clct a,tb_evt_dlv c
&where a.clct_date&=to_date(';,'yyyymmdd') and a.clct_date&=to_date(';,'yyyymmdd')
& and a.mail_num=c.mail_num and c.dlv_sts_code='I'
&and exists (select 1 from tb_jg b where b.zj_code=a.clct_bureau_org_code and b.city ='合肥市')
&and exists (select 1 from sncn_zd_jg d where d.zj_code=c.dlv_bureau_org_code and d.city ='安庆市')
& group by a.clct_date order by a.clct_date) dd
& on aa.rq=dd.rq
(select a.clct_date rq,count(*) zszt from tb_evt_mail_clct a,tb_evt_dlv c
&where a.clct_date&=to_date(';,'yyyymmdd') and a.clct_date&=to_date(';,'yyyymmdd')
& and a.mail_num=c.mail_num and c.dlv_sts_code='I'
&and exists (select 1 from sncn_zd_jg b where b.zj_code=a.clct_bureau_org_code and b.city ='合肥市')
&and exists (select 1 from sncn_zd_jg d where d.zj_code=c.dlv_bureau_org_code and d.city ='安庆市')
& group by a.clct_date order by a.clct_date) ee
& on aa.rq=ee.
附:JOIN语句语法:
JOIN用于根据两个或多个表中的列之间的关系,从这些表中查询数据。
JOIN: 如果表中有至少一个匹配,则返回行
LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
FULL JOIN: 只要其中一个表中存在匹配,就返回行
SQL INNER JOIN 关键字
在表中存在至少一个匹配时,INNER JOIN 关键字返回行。
INNER JOIN 关键字语法
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
注释:INNER JOIN 与JOIN 是相同的。
SQL LEFT JOIN 关键字
LEFT JOIN 关键字会从左表(table_name1) 那里返回所有的行,即使在右表(table_name2) 中没有匹配的行。
LEFT JOIN 关键字语法
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
SQL RIGHT JOIN 关键字
RIGHT JOIN 关键字会右表(table_name2) 那里返回所有的行,即使在左表(table_name1) 中没有匹配的行。
RIGHT JOIN 关键字语法
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
注释:在某些中,RIGHT JOIN 称为RIGHT OUTER JOIN。
SQL FULL JOIN 关键字
只要其中某个表存在匹配,FULL JOIN 关键字就会返回行。
FULL JOIN 关键字语法
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
注释:在某些数据库中,FULL JOIN 称为FULL OUTER JOIN。
摘自 宋哥的专栏
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'sql server 2008 查询结果如何不显示 具体语句就显示结果_百度知道
sql server 2008 查询结果如何不显示 具体语句就显示结果
hiphotos.hiphotos://g.baidu。.com/zhidao/pic/item/6f061d950a7bd361d9f2d3572cc82f://g.baidu。就显示结果如图 红线框的部分不想显示.com/zhidao/wh%3D450%2C600/sign=c5fb797ccbbfac17a2ed8d72/6f061d950a7bd361d9f2d3572cc82f://g.hiphotos
我有更好的答案
area&a&from&nbsp,是把你查的结果插入到了#b中select&#b&as&*&as&from&on&nbsp,直接select&#b这样或者你第一句刚才执行完了.areaidgoselect&nbsp你这不是查询;from&s&*&left&nbsp.id=b;servicepay&a;join&into&customer&100&top&nbsp
我只是不想下面的结果 显示 具体语句。只显示100行受影响那个。
你完整的是怎么写的?
就框框里面的。以前不显示语句的。后来不知道碰到什么了。
其他类似问题
为您推荐:
server的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁博客访问: 1645428
博文数量: 1323
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: Linux
SQL语句中的CASE语句与高级语言中的switch语句,是标准SQL的语法,适用与一个条件判断有多种值的情况下分别执行不同的操作。灵活应用CASE语句可以使SQL语句变得简洁易读,下面在DB2环境下通过一个简单的查询来展示SQL CASE语句的强大功能。
Windows XP Professional
有一个行业代码表,建表SQL和数据如下,要求查出代码别名、代码名、行业名、代码长度。代码别名为数字序号与大写英文字母的序号的映射值,比如代码 '01'的别名就是'A','02'的别名就是'B',依次类推。
建表SQL和初始化数据SQL
-------------------------------------
drop table DM_HYML;
create table DM_HYML
& HYML_DM CHAR(2) not null,
& HYML_MC VARCHAR(100) not null,
& XYBZ&&& CHAR(1) not null
alter table DM_HYML
& add primary key (HYML_DM);
comment on table DM_HYML is
&&& '行业门类代码表';
comment on column DM_HYML.HYML_DM is
&&& '行业门类代码';
comment on column DM_HYML.HYML_MC is
&&& '行业门类名称';
comment on column DM_HYML.XYBZ is
&&& '选用标志';
delete from DM_HYML;
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('01', '农、林、牧、渔业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('03', '制造业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('02', '采矿业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('04', '电力、燃气及水的生产和供应业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('05', '建筑业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('06', '交通运输、仓储和邮政业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('07', '信息传输、计算机服务和软件业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('08', '批发和零售业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('09', '住宿和餐饮业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('10', '金融业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('11', '房地产业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('12', '租赁和商务服务业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('13', '科学研究、技术服务和地质勘查业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('14', '水利、环境和公共设施管理业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('15', '居民服务和其他服务业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('16', '教育', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('17', '卫生、社会保障和社会福利业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('18', '文化、体育和娱乐业', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('19', '公共管理和社会组织', 'Y');
insert into DM_HYML (HYML_DM, HYML_MC, XYBZ)
values ('20', '国际组织', 'Y');
select (case t.hyml_dm
&&&&&&&& when '01' then 'A'
&&&&&&&& when '02' then 'B'
&&&&&&&& when '03' then 'C'
&&&&&&&& when '04' then 'D'
&&&&&&&& when '05' then 'E'
&&&&&&&& when '06' then 'F'
&&&&&&&& when '07' then 'G'
&&&&&&&& when '08' then 'H'
&&&&&&&& when '09' then 'I'
&&&&&&&& when '10' then 'J'
&&&&&&&& when '11' then 'K'
&&&&&&&& when '12' then 'L'
&&&&&&&& when '13' then 'M'
&&&&&&&& when '14' then 'N'
&&&&&&&& when '15' then 'O'
&&&&&&&& when '16' then 'P'
&&&&&&&& when '17' then 'Q'
&&&&&&&& when '18' then 'R'
&&&&&&&& when '19' then 'S'
&&&&&&&& when '20' then 'T'
&&&&&&&& when '21' then 'U'
&&&&&&&& when '22' then 'V'
&&&&&&&& when '23' then 'W'
&&&&&&&& when '24' then 'X'
&&&&&&&& when '25' then 'Y'
&&&&&&&& when '26' then 'Z'
&&&&&& end) as hydmbm,
&&&&&& t.hyml_dm,
&&&&&& t.hyml_mc,
&&&&&& length(t.hyml_dm) as sublenth,
&&&&&& '00' as zb
& from dm_hyml t
& 将此sql代码保存为C:\test.sql文件,在DOS下进入DB2安装目录的bin目录下,链接数据库并执行(命令)此SQL,并重定向输出查询结果和信息到C:\test.txt。
C:\IBM\SQLLIB\BIN>db2& -tvf C:\test.sql > C:\test.txt
执行结果:
打开C:\test.txt文件查看结果:
select (case t.hyml_dm when '01' then 'A' when '02' then 'B' when '03' then 'C' when '04' then 'D' when '05' then 'E' when '06' then 'F' when '07' then 'G' when '08' then 'H' when '09' then 'I' when '10' then 'J' when '11' then 'K' when '12' then 'L' when '13' then 'M' when '14' then 'N' when '15' then 'O' when '16' then 'P' when '17' then 'Q' when '18' then 'R' when '19' then 'S' when '20' then 'T' when '21' then 'U' when '22' then 'V' when '23' then 'W' when '24' then 'X' when '25' then 'Y' when '26' then 'Z' end) as hydmbm, t.hyml_dm, t.hyml_mc, length(t.hyml_dm) as sublenth, '00' as zb from dm_hyml t
HYDMBM HYML_DM HYML_MC&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& SUBLENTH&&& ZB
------ ------- ---------------------------------- ----- --
A&&&&& 01&&&&& 农、林、牧、渔业&&&&&&&&&&&&&&&&&&&&&& 2 00
C&&&&& 03&&&&& 制造业&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 2 00
B&&&&& 02&&&&& 采矿业&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 2 00
D&&&&& 04&&&&& 电力、燃气及水的生产和供应业&&&&&&&&&& 2 00
E&&&&& 05&&&&& 建筑业&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 2 00
F&&&&& 06&&&&& 交通运输、仓储和邮政业&&&&&&&&&&&&&&&& 2 00
G&&&&& 07&&&&& 信息传输、计算机服务和软件业&&&&&&&&&& 2 00
H&&&&& 08&&&&& 批发和零售业&&&&&&&&&&&&&&&&&&&&&&&&&& 2 00
I&&&&& 09&&&&& 住宿和餐饮业&&&&&&&&&&&&&&&&&&&&&&&&&& 2 00
J&&&&& 10&&&&& 金融业&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 2 00
K&&&&& 11&&&&& 房地产业&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 2 00
L&&&&& 12&&&&& 租赁和商务服务业&&&&&&&&&&&&&&&&&&&&&& 2 00
M&&&&& 13&&&&& 科学研究、技术服务和地质勘查业&&&&&&&& 2 00
N&&&&& 14&&&&& 水利、环境和公共设施管理业&&&&&&&&&&&& 2 00
O&&&&& 15&&&&& 居民服务和其他服务业&&&&&&&&&&&&&&&&&& 2 00
P&&&&& 16&&&&& 教育&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 2 00
Q&&&&& 17&&&&& 卫生、社会保障和社会福利业&&&&&&&&&&&& 2 00
R&&&&& 18&&&&& 文化、体育和娱乐业&&&&&&&&&&&&&&&&&&&& 2 00
S&&&&& 19&&&&& 公共管理和社会组织&&&&&&&&&&&&&&&&&&&& 2 00
T&&&&& 20&&&&& 国际组织&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 2 00
& 20 条记录已选择。
呵呵,CASE语句方便吧。
注意:DB2命令行下执行sql语句只能是一行,如果要执行多行,可以将sql保存为文件执行,执行的方法是:
1、执行SQL语句
db2& -tvf [filename].sql
2、执行存储过程
db2 -td@ -vf [filename].sql
当然这些命令的选项根据需要有所不同,可以直接从命令行查看这些选项:db2 ? OPTIONS
&选项&&& 描述&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 缺省设置
&------& ----------------------------------------& ---------------
&& -a&&& 显示 SQLCA&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& OFF
&& -c&&& 自动落实&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ON
&& -d&&& 检索并显示 XML 声明&&&&&&&&&&&&&&&&&&&&&& OFF
&& -e&&& 显示 SQLCODE/SQLSTATE&&&&&&&&&&&&&&&&&&&& OFF
&& -f&&& 读取输入文件&&&&&&&&&&&&&&&&&&&&&&&&&&&&& OFF
&& -i&&& 显示 XML 数据并带有缩进&&&&&&&&&&&&&&&&&& OFF
&& -l&&& 将命令记录到历史记录文件中&&&&&&&&&&&&&&& OFF
&& -n&&& 除去换行字符&&&&&&&&&&&&&&&&&&&&&&&&&&&&& OFF
&& -o&&& 显示输出&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ON
&& -p&&& 显示 db2 交互式提示符&&&&&&&&&&&&&&&&&&&& ON
&& -q&&& 保留空格和换行符&&&&&&&&&&&&&&&&&&&&&&&&& OFF
&& -r&&& 将输出报告保存到文件&&&&&&&&&&&&&&&&&&&&& OFF
&& -s&&& 在命令出错时停止执行&&&&&&&&&&&&&&&&&&&&& OFF
&& -t&&& 设置语句终止字符&&&&&&&&&&&&&&&&&&&&&&&&& OFF
&& -v&&& 回传当前命令&&&&&&&&&&&&&&&&&&&&&&&&&&&&& OFF
&& -w&&& 显示 FETCH/SELECT 警告消息&&&&&&&&&&&&&&& ON
&& -x&&& 不打印列标题&&&&&&&&&&&&&&&&&&&&&&&&&&&&& OFF
&& -z&&& 将所有输出保存到输出文件&&&&&&&&&&&&&&&&& OFF
&使用 DB2OPTIONS 环境变量定制选项缺省值。
&紧跟选项字母后的减号(-)使该选项关闭。
&若将减号(-)更改为加号(+),则选项
&文件输入方式)。
CASE和IF的区别:
在高级语言中,CASE的可以用IF来替代,但是在SQL中不行。
CASE是SQL标准定义的,IF是数据库系统的扩展。
CASE可以用于SQL语句和SQL存储过程、触发器,IF只能用于存储过程和触发器。
在SQL过程和触发器中,用IF替代CASE代价都相当的高,相当的麻烦,难以实现。
CASE语句应用对比:
下面做两组查询,每组用两种方法来实现,一种是用case,一种是不用case,谁快谁获胜,测试环境依然DB2 V9.1、windows server 2003。
第一组:查询dj_zt表状态为'07'或'11'、qylx_dm = '03'的所有记录数。A:用CASE语句select count(case a.zt when '07' then a.bs end)+&&& count(case a.zt when '11' then a.bs end)& from dj_zt a&where a.qylx_dm = '03'----------------11829
B:不用CASE语句select count(*)& from dj_zt a&where a.qylx_dm = '03'&& and a.zt in ('07', '11')----------------11829
结果:A、B两组耗费的代价一样的,相比B的写法简洁,平局。
第二组:分别查询dj_zt表状态为'07'和'11'且qylx_dm = '03'的所有记录数。
A:用CASE语句select count(case a.zt when '07' then a.bs end),&&& count(case a.zt when '11' then a.bs end)& from dj_zt a&where a.qylx_dm = '03----------------
B:不用CASE语句(写了两条语句,扫描表两遍,效率明显低下)select count(*)& from dj_zt a&where a.qylx_dm = '03'&& and a.zt='07'----------------4565&
select count(*)& from dj_zt a&where a.qylx_dm = '03'&& and a.zt='11'----------------7264&
结果:B组代价明显高出A组很多,并且麻烦,A胜!
总结:通过上面两组实例可以看出,灵活应用CASE语句可以让SQL变得简洁高效,而且,CASE的使用一般不会引起性能(相比没有用CASE的语句)低下。
阅读(3083) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
SQL中CASE语句强大功能详解这篇文章已被推荐到圈子中。
请登录后评论。SQL 2008多表查询 如何把不存在的值也显示出来的sql语句怎么写?_百度知道
SQL 2008多表查询 如何把不存在的值也显示出来的sql语句怎么写?
如两张表:查询结果为:&&&&&&&&&
根据一个uid来查询并且NULL也要出来。 这是我自己写的:select
f.title,u.fid,u.uid from
left join u on f.fid=u.fid
where u.uid=1997是否可穿酣扁叫壮既憋习铂卢以查处我想要上面图片的结果?
提问者采纳
select a.title, case when b.uid is null then 'null' else a.fid , b.uidfrom a left join b 穿酣扁叫壮既憋习铂卢on a.fid=b.fid
如果加个条件 b.uid=1997
是否可以把上面的空值也显示出来?
不行,不过你可以试试这个select a.title, b.fid, b.uidfrom a left join (select * from b where b.uid=1997) b on a.fid=b.fid
提问者评价
非常感谢 要的就是这个结果!
其他类似问题
为您推荐:
其他1条回答
select title,a.fid,uid from a left join穿酣扁叫壮既憋习铂卢 b on a.fid=b.fid
这样查询出来的结果把两个uid都出来了,是否可以根据一个uid查询出来并且显示NULL的值?
select title,id=case when b.uid is not null then a.fid else null end,uid from a left join b on a.fid=b.fid 不知道这是不是你要的。
sql语句的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 sql server基本语句 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信