【问题标题】:How to do row-wise comparison between SQL (Oracle) fields?如何在 SQL (Oracle) 字段之间进行逐行比较?
【发布时间】:2015-08-13 01:24:33
【问题描述】:

以下表为例:

-------------------------
| time      | start_time|
-------------------------
|  50       | 80        |
|  20       | 72        |
|  45       | 30        |
-------------------------

我想要一个返回所有行的查询,其中time 小于相应行中给出的start_time,这个查询应该返回:

-------------------------
| time      | start_time|
-------------------------
|  50       | 80        |
|  20       | 72        |
-------------------------

【问题讨论】:

    标签: sql oracle select compare


    【解决方案1】:
    select *
    from tablename
    where time < start_time
    

    假设两列都是数字,您可以试试这个。

    【讨论】:

    • 谢谢。如果timestart_time 的行数不同,运算符&lt; 的行为会如何?
    • @Alex.. 我不确定我明白你在说什么? time and start_time did not have the same number of rows?
    • 我只是对&lt; 操作员的行为感到好奇。因此,如果 RHS 是标量,它将所有行与标量进行比较,并且如果 RHS 是与 LHS 具有相同行数的列,则根据您的答案和我的示例,它会进行逐行比较。但是,如果两边的条目数不匹配怎么办?
    • 这只是您提到的任何一种情况。我现在不能,想想两边的条目数不匹配的情况。
    • 例如,如果第二面是使用子查询获得的。
    【解决方案2】:

    如果你想比较标量和集合。您可以使用“any”或“all”关键字。但我不确定你是否问过这个问题。 ALL, ANY and SOME Comparison Conditions in SQL

    select * from temp_table where 
    time < any (select start_time from temp_table)
    
    select * from temp_table where 
    time < all (select start_time from temp_table)
    

    【讨论】:

    • 不是我问的,但这对了解很有用,谢谢。
    猜你喜欢
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 2021-04-27
    • 1970-01-01
    相关资源
    最近更新 更多