在查询中使用if / else语句

时间:2020-11-03 15:03:53

标签: mysql

我有以下查询,我想将o.interval的秒数转换为几天,但前提是该行的o.interval_type设置为“ interval”。如果将其设置为“ day”,那么我只想输出o.interval

SELECT o.id as "Sub ID" ,
o.customer_id as "Customer ID",
o.status as "Status",
FROM_UNIXTIME(o.start_date) as "Start Date",
FROM_UNIXTIME(o.last_processed) as "Last Processed",
o.interval_type as "Interval Type",
CONCAT(FLOOR(HOUR(SEC_TO_TIME(o.interval)) / 24)) as "Interval"
FROM subscriptions as o

如何合并if / else语句?

1 个答案:

答案 0 :(得分:1)

IF(
  o.interval_type = 'interval',
  CONCAT(FLOOR(HOUR(SEC_TO_TIME(o.interval)) / 24)),
  o.interval
) AS "Interval"

如果条件(o.interval_type = 'interval')为true,则该表达式的值为CONCAT(FLOOR(HOUR(SEC_TO_TIME(o.interval)) / 24)),否则为o.interval