文档详情

SQL数据库基础讲解SELECT、SELECT小技巧.ppt

发布:2017-03-28约字共12页下载文档
文本预览下载声明
SQL数据库基础讲解 主讲人:赵峰 SELECT 格式:SELECT 字段名 FROM 表名 WHERE 条件 例:查询数据库中所有10月份类别为01的凭证 SELECT * FROM GL_ACCVOUCH WHERE IPERIOD=10 AND CSIGN=‘01’ SELECT *代表所有字段 GL_ACCVOUCH为凭证表名 IPERIOD 是月 CSIGN是凭证类别 查某几个字段可以把*换成字段名 可以用BETWEEN AND 来规定日期范围 可以用IN()来规定条件范围 SELECT 在总帐中查询所有客户10月20日到11月20日的应收帐款发生额及余额,按客户编码排序 Select sum(md)”借方金额” ,sum(mc)”贷方金额”,sum(md)-sum(mc)”余额” from gl_accvouch where ddate between ‘2002-10-20’ and ‘2002-11-20’ and ccode=‘1131’ group by ccus_id order by ccus_id SELECT Sum为求和,sum和group by 连用是分组求和,如果不用group by 就是把所有记录加成一个数 order by 为排序 order by +字段名就是按某一字段排序 SELECT 两个表关联查询 格式:select 字段名 from 表名a join 表名 b on 关联条件 where 查询条件 例:select b.ccusname”客户名称”,b.ccusid”客户编码”,a.sum(md)”借方金额”,a.sum(mc)”贷方金额” from gl_accvouch a inner join customer b on a.ccus_id=b.ccusid where a.ddate between ‘2002-10-20’ and ‘2002-11-20’ and a.ccode=‘1131’ group by b.ccusid order by b.ccusid SELECT Inner join 内连接 只显示查询字段 left join 左连接 两张表的字段同时显示 SELECT小技巧 select a.citem_id,b.cinvstd, sum(case when cendd_c=借 then me else 0 end)-sum(case when cendd_c=贷 then me else 0 end) as je, sum(case when cendd_c=借 then ne_s else 0 end)- sum(case when cendd_c=贷 then ne_s else 0 end) as ne_s, sum(b.iinvsprice/1.17) as wsdjinto ch_temp from gl_accass a join inventory b on a.citem_id=b.cinvcode where iperiod=10 and citem_id is not null and ccode=124101 and me0group by a.citem_id,b.cinvstd SELECT 小技巧 case when 条件 then 结果 else 另一结果.如果where 条件里只能加入一种选择的话,以上语句就十分有用了,它可以帮助我们分类统计数据 select 字段名 into 临时表名 from 来源表名 where 条件,如果我们的数据要从好多表中提取,那用生成临时表的方法会使我们少绕很多弯路. SELECT 小技巧 跨计算机取数 select a.ccode from gl_accass a left join [192.1.5.252].[haier_office].[dbo].b_queryparam b on a.ccode=b.ccode where b.ccode is null group by a.ccode sp_serveroption 192.1.5.252,data access,true SELECT 小技巧 查找出ap_detail中cpzid在gl_accvouch中不存在的记录 查某一记录在另一张相关联的表里不存在时用not in select* from ap_detail where cpzid not in (select coutno_id from gl_accvouch a inner join ap_detail b on a.coutno_id=b.cpzid) SELECT 小技巧 Dist
显示全部
相似文档