如何根据列的值使用 sql 不同的 where 语句?

How to use sql different where statement on the basis of column's value?

我有 table 命名为 offers。它有以下列

  1. is_special 的值为 true 或 false
  2. normal_expiry_date 具有日期值
  3. special_expiry_date 具有日期值

现在,如果行有 is_special = false 那么我想使用 where normal_expiry_date > current_date,如果 is_special = true 然后 where special_expiry_date > current_date

非常感谢你,即使你试图提供帮助

您可以使用布尔逻辑:

where 
    (is_special = 'false' and normal_expiry_date > current_date)
    or (is_special = 'true' and special_expiry_date  > current_date)

注意:is_specia 的数据类型不清楚,因此您可能需要调整相等条件。例如,如果这是一个 Postgres 布尔值:

where 
    (not is_special and normal_expiry_date > current_date)
    or (is_special and special_expiry_date  > current_date)

一个选项是

where current_date < case when is_special = false then normal_expiry_date
                          when is_special = true  then special_expiry_date
                     end