我有以下查询:
SELECT IF(TIMESTAMPDIFF(HOUR,users_codes.timestam,CURRENT_TIMESTAMP()) < 48,'no','yes') AS Expired
FROM users_codes
如何仅返回具有的行'no'
?
添加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'
我相信你可以做这样的事情
SELECT IF( TIMESTAMPDIFF(
HOUR,
users_codes.timestam,
CURRENT_TIMESTAMP()
) < 48, 'no', 'yes') AS Expired
FROM users_codes
HAVING Expired = 'no';