3

我有以下查询:

SELECT IF(TIMESTAMPDIFF(HOUR,users_codes.timestam,CURRENT_TIMESTAMP()) < 48,'no','yes') AS Expired
FROM users_codes

如何仅返回具有的行'no'

4

2 回答 2

6

添加WHERE条件。您不能ALIAS在 where 子句上使用。

SELECT IF(TIMESTAMPDIFF(HOUR,users_codes.timestam,CURRENT_TIMESTAMP()) < 48,'no','yes') AS Expired 
FROM   users_codes
WHERE  IF(TIMESTAMPDIFF(HOUR,users_codes.timestam,CURRENT_TIMESTAMP()) < 48,'no','yes') = 'no'

或使用子查询

SELECT *
FROM
(
    SELECT IF(TIMESTAMPDIFF(HOUR,users_codes.timestam,CURRENT_TIMESTAMP()) < 48,'no','yes') AS Expired 
    FROM   users_codes
) s
WHERE  Expired  = 'no'
于 2012-12-11T05:12:39.950 回答
2

我相信你可以做这样的事情

SELECT IF( TIMESTAMPDIFF(
    HOUR,
    users_codes.timestam,
    CURRENT_TIMESTAMP()
  ) < 48, 'no', 'yes') AS Expired

FROM users_codes
HAVING Expired = 'no';
于 2012-12-11T05:14:26.317 回答