【问题标题】:SQL WHERE IN CLAUSESQL WHERE 子句
【发布时间】:2014-03-20 20:30:03
【问题描述】:

我写作是为了解决一个特定的 SQL 查询。

这是我的情况:以何种方式可以执行在 WHERE 子句中允许识别(从另一个表)一个字段的相等性和另一个字段的 > 的查询?

类似于 IN 子句:

select * 
from tableX
WHERE (tableX.field1, tableX.field2) IN (select tableY.field1, tableY.field2 from tableY)

就我而言,当tableX.field2 = tableY.field2 时,我需要识别所有大于field1Yfiled1X

谢谢。

尼哥

【问题讨论】:

    标签: sql oracle11g where-clause


    【解决方案1】:
    select tableX.field1 
    from tableX innerjoin tableY on tableX.field2 = tableY.field2
    where tableX.field1 > tabley.field1
    

    【讨论】:

    • 联接不一定是IN 查询的替代品。如果tableY 中有不止一行满足连接条件怎么办?
    • @a_horse_with_no_name 我不认为 N​​ico 要求替换 IN 查询:他只是想要 tableX.field1 的所有实例大于 tabley.field2 其中 tablex.field 2 等于tabley.field 2(因此加入)。
    【解决方案2】:
    select * 
    from tableX
    WHERE exists (select 42 
                  from tabley 
                  where tableX.field2 = tableY.field2
                    and tableX.field1X > tableY.field1Y)
    

    【讨论】:

      【解决方案3】:

      试试这个:

      select *
      from tableX as t1, tableY as t2
      where(t1.field1 = t2.field1) AND (t1.field2 > t2.field2)
      

      【讨论】:

        【解决方案4】:

        您想要加入。

        SELECT x.* 
        FROM tableX x
        JOIN tableY y on x.field2 = y.field2 AND x.field1 > y.field1
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-30
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-17
          • 2022-01-20
          相关资源
          最近更新 更多