繁体   English   中英

如何将SQL连接语法应用于旧连接

[英]How to apply the SQL join syntax to legacy join

当我将遗留连接语法重构为ANSI连接语法时,我遇到了问题,因为我们数据库中的大多数过程都使用了旧连接。 这是我需要更改它以使用SQL'JOIN'语法而不是普通的旧连接的代码。 任何人都可以建议我怎么做?

    select 
          a.userkey, 
          a.username,
          c.monthd ,
          b.currencykey 
     from 
         #users a,
         #invoicedata b,
          #revdate c
     where 
         (a.userkey >= b.userkey or a.userkey <= b.userkey) 
          and b.idate between c.startdate and c.enddate
      group by 
           a.userkey,
           a.username,
           c.monthd,
           b.currencykey

       order by c.id,a.username,b.currencykey

此部分(a.userkey >= b.userkey or a.userkey <= b.userkey)似乎不正确,您不能order byorder by (至少没有agregate函数),不在group by

所以,可能是这样的:

select 
      a.userkey, 
      a.username,
      c.monthd ,
      b.currencykey 
 from #users a
 inner join #invoicedata b on a.userkey = b.userkey
 inner join #revdate c on b.idate between c.startdate and c.enddate
 group by 
      a.userkey,
      a.username,
      c.monthd,
      b.currencykey

我也用内部连接替换了嵌套连接但是没有必要:)


另一方面,您可能需要交叉连接:

select 
      a.userkey, 
      a.username,
      c.monthd ,
      b.currencykey 
 from #users a
 cross join #invoicedata b
 inner join #revdate c on b.idate between c.startdate and c.enddate
 group by 
      a.userkey,
      a.username,
      c.monthd,
      b.currencykey

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM