• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

[Oracle]案例讨论:Oracle两表连接

发布: 2007-6-21 12:06 | 作者:   | 来源:   | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网

   

    Oracle的两表连接怎么实现,原来是SQL的现在想换成oracle的,SQL的如下:


    SELECT COUNT(*) AS COUNT, SUM(timelen) AS totlekeeptime, SUM(moneys) AS totletimefee, SUM(realmoneys) AS totlenewfee, areacode.area_name FROM phonedata INNER JOIN areacode ON LEFT(phonedata.ani, LEN(areacode.area_code)) = areacode.area_code WHERE starttime>2005-12-27 00:00:00 and starttime<2005-12-27 23:59:59 and endtime is not null and userid like 001136 GROUP BY areacode.area_name order by totlekeeptime


    oracle里该怎么办呢?


    讨论一:


    SELECT AREACODE.AREA_NAME,COUNT(*) AS COUNT,


    SUM(TIMELEN) AS TOTLEKEEPTIME,


    SUM(MONEYS) AS TOTLETIMEFEE,


    SUM(REALMONEYS) AS TOTLENEWFEE,


    FROM PHONEDATA INNER JOIN AREACODE ON (LEFT(PHONEDATA.ANI,LEN(AREACODE.AREA_CODE)) = AREACODE.AREA_CODE)


    WHERE STARTTIME>2005-12-27 00:00:00


    AND STARTTIME<2005-12-27 23:59:59


    AND ENDTIME IS NOT NULL


    AND USERID LIKE 001136%


    GROUP BY AREACODE.AREA_NAME


    ORDER BY TOTLEKEEPTIME


    /


    说明:只是不知道你没有限定哪个表的那些字段是不是只有一个表里面有,如果不是,会报错的。


    点评:有错误:


    1.oracle里两表连接不是用INNER JOIN AREACODE ON 而是用外连接LEFT OUTER JOIN …… ON或内连接LEFT in JOIN …… ON


    2.oracle里没有LEFT函数,取字接数是用length;


    讨论二:


    SELECT AREACODE.AREA_NAME,COUNT(*) AS COUNT,


    SUM(TIMELEN) AS TOTLEKEEPTIME,


    SUM(MONEYS) AS TOTLETIMEFEE,


    SUM(REALMONEYS) AS TOTLENEWFEE,


    FROM PHONEDATA ,AREACODE


    where substring(PHONEDATA.ANI,1,length(AREACODE.AREA_CODE)) = AREACODE.AREA_CODE


    and to_date(STARTTIME,yyyy-mm-dd hr24:mi:ss)>2005-12-27 00:00:00


    AND to_date(STARTTIME,yyyy-mm-dd hr24:mi:ss)<2005-12-27 23:59:59


    AND ENDTIME IS NOT NULL


    AND USERID LIKE 001136%


    GROUP BY AREACODE.AREA_NAME


    ORDER BY TOTLEKEEPTIME


    点评:两表连接那错了,但这地方: substring(PHONEDATA.ANI,1,length(AREACODE.AREA_CODE)) = AREACODE.AREA_CODE是对的;


    最佳答案:


    SELECT COUNT(*) AS COUNT, SUM(timelen) AS totlekeeptime, SUM(moneys) AS totletimefee,


    SUM(realmoneys) AS totlenewfee, areacode.area_name


    FROM phonedata ,areacode


    WHERE starttime>2005-12-27 00:00:00 and starttime<2005-12-27 23:59:59


    and endtime is not null and userid like 001136


    /*and LEFT(phonedata.ani, length(areacode.area_code)) = areacode.area_code */


    and substr(phonedata.ani, 1,length(areacode.area_code)) = areacode.area_code


    GROUP BY areacode.area_name order by totlekeeptime


    说明:inner join 直接相等就可以了,left换成substr.

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网