【问题标题】:ORACLE SQL query to return a value from a row where strings of rows in other tables matchORACLE SQL 查询从其他表中的行字符串匹配的行返回值
【发布时间】:2019-11-25 08:39:50
【问题描述】:

我正在尝试让它工作,它应该很简单,但我无法确定它。我只需要返回 num 的值,其中字符串在同一个表和另一个表中匹配。

表 1

string    num
-------   -----
xyzxy       100

表 2

string     othernum
--------   --------
xyzxy       200


SELECT num FROM table1 WHERE (SELECT string FROM table1 = SELECT string FROM table2)

【问题讨论】:

  • 添加更多示例表数据,并指定预期结果。

标签: sql oracle subquery


【解决方案1】:

使用exists()

select num from table1 t1 where exists (select 1 from table2 where string = t1.string)

【讨论】:

    【解决方案2】:

    您也可以使用以下查询:

    SELECT t1.num FROM table1 t1 join table2 t2 
    WHERE t1. string = t2.string;
    

    【讨论】:

      【解决方案3】:

      用在(?):

      SELECT num 
      FROM table1 t1 
      WHERE t1.string in (SELECT string FROM table2)
      

      这应该可行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-31
        • 2017-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-02
        相关资源
        最近更新 更多