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

我应该如何在SQL中查询三个指标a、b、c并验证它们满足(a+b)/2=c

  •  0
  • YuboLiang  · 技术社区  · 3 年前

    现在我想查询数据库中的三个指标a、b和c,并验证公式(a+b)/2=c是否成立。我该怎么办?我现在只能查询a、b和c

    select
        a,
        b,
        c
    from
        table_test
    where
        type = 1
        and
        origin not in (10, 20)
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   Tim Biegeleisen    3 年前

    在中添加断言 WHERE 条款:

    SELECT a, b, c, IF((a + b) / 2 = c, 'True', 'False') AS result
    FROM table_test
    WHERE type = 1 AND origin NOT IN (10, 20) AND
          (a + b) / 2 = c;   -- added condition here