【问题标题】:Sql joins and queries [closed]Sql 连接和查询 [关闭]
【发布时间】:2014-07-18 16:40:55
【问题描述】:

我在 sybase 中有 2 个表

表 1:

ID   CUR
---- ------
A    USD      
A    GBP      
A    HKD      
A    JPY       
A    CHF      
B    USD     
B    GBP

表 2:

Pair     Base  Under
-------- ----- -----
USD/GBP  USD   GBP  
GBP/HKD  GBP   HKD  
USD/JPY  USD   JPY 

如果 table1 中的 ID 同时存在 base 和 Under ,我需要一个查询帮助,该查询将为每个 ID 返回一对货币。

【问题讨论】:

  • 您可以向我们展示您不成功的尝试。

标签: sql join sybase


【解决方案1】:

使用这个查询。

select  Pair  from Table2
where Base in (select CUR from Table1 ) 
and Under in (select CUR from Table1) 

【讨论】:

  • 我认为在基本连接和下连接中都需要相同的 id..
【解决方案2】:
Select distinct pair From table2
    join table1 b 
       on b.CUR = t.bas
    join table1 u 
       on u.id = b.id 
          and u.CUR = t.under

【讨论】:

  • 感谢您的解决方案。您知道我是否可以使用 where 子句。我需要根据 u.id 值返回条目。
  • 是的,你可以在最后添加一个 where 子句
猜你喜欢
  • 2021-01-28
  • 2016-04-11
  • 1970-01-01
  • 2020-03-31
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多