我想构造一个SELECT语句,它可以根据条件命名结果。为了更好地解释我的尝试,我构造了以下无效/非工作语句。如何将下面的SELECT语句更改为真正适合我的原因的语句。谢谢!

SELECT
  CASE
     WHEN dayStart LIKE '2012-06-11%' THEN UNIX_TIMESTAMP(dayStart) as today
     WHEN dayStart LIKE '2012-06-10%' THEN UNIX_TIMESTAMP(dayStart) as yest
     WHEN dayStart LIKE '2012-06-12%' THEN UNIX_TIMESTAMP(dayStart) as tomor
  END
FROM dayBook
WHERE userID = 1

最佳答案

为什么不返回两列呢,一列告诉你日期,另一列告诉你是今天、昨天还是明天?

SELECT
  UNIX_TIMESTAMP(dayStart) as yourDate
, CASE
     WHEN dayStart LIKE '2012-06-11%' THEN 'today'
     WHEN dayStart LIKE '2012-06-10%' THEN 'yest'
     WHEN dayStart LIKE '2012-06-12%' THEN 'tomor'
  END as dayType
FROM dayBook
WHERE userID = 1

08-26 18:51