代码之家  ›  专栏  ›  技术社区  ›  Aura

SQL中特定实例的列比较

  •  0
  • Aura  · 技术社区  · 6 年前

    我有一个样本数据,我想验证一下 Age_Low 小于 Age_High 只是为了一个特定的例子。

      ID   NAME   Age_Low   Age_High
      1    ABC     50         100
      1    ABC     25          60
      1    ABC     75          90
      2    XYZ     20          40
      2    XYZ     50          20
    

    年龄低 小于 ,否则应返回。

    年龄低 在几排。

      SELECT *
      FROM tablename
      WHERE Age_Low > Age_High
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   Nick SamSmith1986    6 年前

    如果我正确地理解了您的问题,您需要选择其中的行 Age_Low Age_High 为了这个价值 ID . 如果是,此查询将执行您想要的操作:

    SELECT *
    FROM tablename t
    WHERE Age_Low > (SELECT MIN(Age_High)
                     FROM tablename t1
                     WHERE t1.ID = t.ID)
    

    ID  NAME    Age_Low     Age_High
    1   ABC     75          90
    2   XYZ     50          20
    
        2
  •  1
  •   Yogesh Sharma    6 年前

    你似乎想要:

    select t.*
    from table t
    where not exists (select 1 from table t1 where t1.id = t.id and t1.name = t.name and t1.Age_Low < t.Age_High);
    
        3
  •  0
  •   Robert Columbia yusuf dalal    6 年前

    试试这个:

    select * from t where Age_Low > 
    (
    
    select min(Age_High) from t where NAME='ABC'
    )