暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SQL语句在VBA、MYSQL、ORACLE条件使用区别

浅显物语 2021-01-22
704

MYSQL:

select 字段1,字段2,  if(条件,条件满足返回值,条件不满足返回值)  as  字段3  from 表  where  ……group  by ……

字段3的AS,可省略


若涉及子查询,需要自定义名

select   字段1,字段2,字段3 from

       (select   字段1,字段2, if(条件,条件满足返回值,条件不满足返回值)  as  字段3  

from 表            
where  ……
group  by …… )  as  b

group by  ……

子查询的as不可省略



oracle:

  select  字段1,字段2,decode(条件,条件满足返回值,条件不满足返回值) 字段3  from 表 where ……group by ……

字段3的AS,可省略


子查询可不用自定义名,子查询可以多表直接交叉,示例

select sl.loc 位置, sku.prod_code 货号, sku.descr 说明, sl.qty 现有量,

      sl.qtyallocated 分配量,ceil(sysdate-sl.editdate+1/3) 未动销天数,sku.category1 品类

from skuxloc sl

join sku on sku.storerkey = sl.storerkey and sl.sku = sku.sku

where qty > 0 and sl.locationtype='PICK' and  sku.category1 in ('皮具','童装') and 

      exists (select 1 from

                      ( select storerkey,sku from skuxloc t where qty > 0 and  t.locationtype='PICK' group by storerkey,sku having count(1) > 1 ) a

                        where a.sku = sku.sku and a.storerkey = sku.storerkey and sl.qtyallocated<>sl.qty)

order by sku.sku





VBA_SQL:


select   字段1,字段2,iif(条件,条件满足返回值,条件不满足返回值)  as 字段3   from [表$]  group by ……

字段3的AS,不可省略


子查询可不用自定义名,但每次次只能套一个子查询,交叉表一次仅一个,示例:


      asql = "select a.分公司,a.物料编号,a.单位,b.体积*sum(a.开单量) as 体积,sum(a.开单量) as 数量 from [SAP元数据$] as a    " _

         & "left join (select DISTINCT 货号,单位,max(体积) as 体积 from [货号体积$] WHERE 体积>=1  GROUP BY 货号,单位 ) as b on b.货号=a.物料编号 and b.单位=a.单位    " _

         & "where  a.开单量>=1  " _

         & "group by a.分公司,a.物料编号,a.单位,b.体积  "

         

      asql = "select 分公司,sum(数量) as 总数量,round(sum(体积)/1000000,2) as 总体积M3 from (" & asql & ")   where 数量>=1 group by 分公司  "

        bsql = "select a.分公司,left(right(a.物料描述,2),1) & '鞋' as 类别,b.参照季节 as 季节,a.单位 as 型号,sum(a.开单量) as 数量,a.开单日期 from [SAP元数据$]   as a  " _

                    & "left join  [季节$] as b on b.季节代码=a.季节       " _

                    & "group by a.分公司,left(right(a.物料描述,2),1),b.参照季节,a.单位,a.开单日期  " _

                    & "order by  a.分公司,left(right(a.物料描述,2),1),b.参照季节,a.单位         "

        

        csql = "select aa.分公司,count(aa.分公司) as 订单数量,bb.总数量,bb.总体积M3 from (" & bsql & ")  as aa    " _

            & "left join (" & asql & ") as bb on bb.分公司=aa.分公司 " _

            & " group by aa.分公司,bb.总数量,bb.总体积M3 "

        

        

        bsql = "select a.分公司,b.分公司代码,b.品牌,b.承运商,b.承运商代码,iif(b.计划单时间='原数据里的开单日期','原数据里的开单日期',format(b.计划单时间,'yyyy.mm.dd')) as 计划单时间, " _

                & "      b.提货点,b.提货时间,a.类别,a.季节,a.型号,a.数量,format(a.开单日期,'yyyy.mm.dd') as 开单日期 from (" & bsql & " ) as a  " _

               & "left join [承运对照$] as b on b.备注=a.分公司  "

        ssql = "select a.分公司,a.分公司代码,a.品牌,a.承运商,a.承运商代码,iif(a.计划单时间='原数据里的开单日期',a.开单日期,a.计划单时间) as  计划单时间,   " _

               & "     a.提货点,format(a.提货时间,'hh:mm') as 提货时间,b.订单数量,b.总数量,b.总体积M3,a.类别,a.季节,a.型号,a.数量 from (" & bsql & " ) as a  " _

               & "left join (" & csql & " ) as b on b.分公司=a.分公司 "


注,在VBA开发工具中,跨行需要拼接符&进行拼接,而MYSQL与ORACLE语句直接回车跨行即可;

但如若MYSQL与ORACLE在VBA编辑开发工具中,引用,则需要用同样的方法进行&字符拼接;


以上为,三种SQL语句应用区别的简述,更多的需要实践中,不断去应用才能发现各自的区别,以及各自互相切换着应用时,如果左右手互换着来。

文章转载自浅显物语,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论